##### R CODE TO REPLICATE THE TABLES INCLUDED IN 
##### Dawn Brancati and Adrian Lucardi, "Why Democracy Protests Do Not Diffuse," Journal of Conflict Resolution, 63(10), 2019, 2354-2389.

## Loading the required R packages. Make sure to have them installed before proceeding. In order to install a package called "X", write install.packages("X")
library (car)
library (caTools)
library (cshapes)
library (dplyr)
library (ggplot2)
library (hrbrthemes)
library (lfe)
library (lme4)
library (plyr)
library (plm)
library (readstata13)
library (sp)
library (spData)
library (spdep)
library (survival)
library (xtable)

## display options
options (digits=4, scipen=6, show.signif.stars=FALSE, tibble.print_max = Inf)

## working directory -> change this to the directory where your have your data
setwd ("/Users/adrianlucardi/Documents/WashU/Research/Published/2019 JCR Democracy protests/Analysis/Replication/")

## functions for calculating minimum and maximum values when there are NA's
max.na <- function (x) {
  if (all (is.na (x))==TRUE){ return (NA) 
  } else {return (max (x, na.rm=TRUE))} 
}
min.na <- function (x) {
  if (all (is.na (x))==TRUE){ return (NA) 
  } else {return (min (x, na.rm=TRUE))} 
}

## function for calculating the mode
getmode <- function (x){
  uniqx <- unique (x)
  uniqx[which.max (tabulate (match (x, uniqx)))]
}


## importing the data from running the models

# results from statistical analysis
system.time (load ("Replication Results BrancatiLucardi JCR Democracy Protests.RData")) ## 385 seconds

# distances between countries
distances <- read.dta13 ("Distances 1989-2011_tolerance0005.dta")
distances$mindist <- round (distances$mindist, 0)





########## (1) MAIN TEXT

### (1.1) Data/Values mentioned in the text

## (1.1.1) total number of observations and democracy protests (p. 13 of the manuscript)
(sumY <- sum (base$pbega))  ## 289 protests
(N <- nrow (base)) ## approx. 1.4 million observations


## (1.1.2) countries with no neighbors within 50 or 800km (fn. 8)
distances$d50 <- with (distances, ifelse (country==country2, NA, ifelse (mindist <= 50, 1, 0)))
distances$d800 <- with (distances, ifelse (country==country2, NA, ifelse (mindist <= 800, 1, 0)))

dExclude <- with (distances, aggregate (cbind (d50, d800) ~ country, FUN=max.na))
dExclude[dExclude$d50==0,]; nrow (dExclude[dExclude$d50==0,]) ## 17 countries
sort (dExclude[dExclude$d50==0,]$country)
# [1] Australia             Bahamas               Barbados              Cape Verde           
# [5] Comoros               Cuba                  Cyprus                Fiji                 
# [9] Iceland               Jamaica               Madagascar            Maldives             
# [13] Malta                 Mauritius             New Zealand           Sao Tome and Principe
# [17] Taiwan   

dExclude[dExclude$d800==0,]; nrow (dExclude[dExclude$d800==0,]) ## just 2
sort (dExclude[dExclude$d800==0,]$country)
# [1] Fiji      Mauritius

# identifying World regions according to the UN system
clist <- as.data.frame (sort (unique (distances$country)))
colnames (clist) <- "country"
clist$region <- as.character (clist$country)

clist$region <- sub ("Burundi", replacement="East Africa", clist$region)
clist$region <- sub ("Comoros", replacement="East Africa", clist$region)
clist$region <- sub ("Djibouti", replacement="East Africa", clist$region)
clist$region <- sub ("Eritrea", replacement="East Africa", clist$region)
clist$region <- sub ("Ethiopia", replacement="East Africa", clist$region)
clist$region <- sub ("Kenya", replacement="East Africa", clist$region)
clist$region <- sub ("Madagascar", replacement="East Africa", clist$region)
clist$region <- sub ("Malawi", replacement="East Africa", clist$region)
clist$region <- sub ("Mauritius", replacement="East Africa", clist$region)
clist$region <- sub ("Mayotte", replacement="East Africa", clist$region)
clist$region <- sub ("Mozambique", replacement="East Africa", clist$region)
clist$region <- sub ("R??union", replacement="East Africa", clist$region)
clist$region <- sub ("Rwanda", replacement="East Africa", clist$region)
clist$region <- sub ("Seychelles", replacement="East Africa", clist$region)
clist$region <- sub ("Somalia", replacement="East Africa", clist$region)
clist$region <- sub ("South Sudan", replacement="East Africa", clist$region)
clist$region <- sub ("Uganda", replacement="East Africa", clist$region)
clist$region <- sub ("Tanzania", replacement="East Africa", clist$region)
clist$region <- sub ("Zambia", replacement="East Africa", clist$region)
clist$region <- sub ("Zimbabwe", replacement="East Africa", clist$region)

clist$region <- sub ("Angola", replacement="Middle Africa", clist$region)
clist$region <- sub ("Cameroon", replacement="Middle Africa", clist$region)
clist$region <- sub ("Central African Republic", replacement="Middle Africa", clist$region)
clist$region <- sub ("Chad", replacement="Middle Africa", clist$region)
clist$region <- sub ("Congo, Democratic Republic of", replacement="Middle Africa", clist$region)
clist$region <- sub ("Equatorial Guinea", replacement="Middle Africa", clist$region)
clist$region <- sub ("Gabon", replacement="Middle Africa", clist$region)
clist$region <- sub ("Sao Tome and Principe", replacement="Middle Africa", clist$region)
clist$region <- sub ("Congo, Republic of", replacement="Middle Africa", clist$region)

clist$region <- sub ("Algeria", replacement="North Africa", clist$region)
clist$region <- sub ("Egypt", replacement="North Africa", clist$region)
clist$region <- sub ("Libya", replacement="North Africa", clist$region)
clist$region <- sub ("Morocco", replacement="North Africa", clist$region)
clist$region <- sub ("Sudan", replacement="North Africa", clist$region)
clist$region <- sub ("Tunisia", replacement="North Africa", clist$region)
clist$region <- sub ("Western Sahara", replacement="North Africa", clist$region)

clist$region <- sub ("Botswana", replacement="South Africa", clist$region)
clist$region <- sub ("Lesotho", replacement="South Africa", clist$region)
clist$region <- sub ("Namibia", replacement="South Africa", clist$region)
clist$region <- sub ("South Africa", replacement="South Africa", clist$region)
clist$region <- sub ("Swaziland", replacement="South Africa", clist$region)

clist$region <- sub ("Benin", replacement="West Africa", clist$region)
clist$region <- sub ("Burkina Faso", replacement="West Africa", clist$region)
clist$region <- sub ("Cape Verde", replacement="West Africa", clist$region)
clist$region <- sub ("Ivory Coast", replacement="West Africa", clist$region)
clist$region <- sub ("The Gambia", replacement="West Africa", clist$region)
clist$region <- sub ("Ghana", replacement="West Africa", clist$region)
clist$region <- sub ("Guinea-Bissau", replacement="West Africa", clist$region)
clist$region <- sub ("Liberia", replacement="West Africa", clist$region)
clist$region <- sub ("Mali", replacement="West Africa", clist$region)
clist$region <- sub ("Mauritania", replacement="West Africa", clist$region)
clist$region <- sub ("Nigeria", replacement="West Africa", clist$region)
clist$region <- sub ("Saint Helena", replacement="West Africa", clist$region)
clist$region <- sub ("Senegal", replacement="West Africa", clist$region)
clist$region <- sub ("Sierra Leone", replacement="West Africa", clist$region)
clist$region <- sub ("Togo", replacement="West Africa", clist$region)
clist$region <- sub ("Niger", replacement="West Africa", clist$region)

clist$region <- sub ("Anguilla", replacement="Caribbean", clist$region)
clist$region <- sub ("Antigua and Barbuda", replacement="Caribbean", clist$region)
clist$region <- sub ("Aruba", replacement="Caribbean", clist$region)
clist$region <- sub ("Bahamas", replacement="Caribbean", clist$region)
clist$region <- sub ("Barbados", replacement="Caribbean", clist$region)
clist$region <- sub ("Bonaire, Sint Eustatius and Saba", replacement="Caribbean", clist$region)
clist$region <- sub ("British Virgin Islands", replacement="Caribbean", clist$region)
clist$region <- sub ("Cayman Islands", replacement="Caribbean", clist$region)
clist$region <- sub ("Cuba", replacement="Caribbean", clist$region)
clist$region <- sub ("Cura??ao", replacement="Caribbean", clist$region)
clist$region <- sub ("Dominican Republic", replacement="Caribbean", clist$region)
clist$region <- sub ("Grenada", replacement="Caribbean", clist$region)
clist$region <- sub ("Guadeloupe", replacement="Caribbean", clist$region)
clist$region <- sub ("Haiti", replacement="Caribbean", clist$region)
clist$region <- sub ("Jamaica", replacement="Caribbean", clist$region)
clist$region <- sub ("Martinique", replacement="Caribbean", clist$region)
clist$region <- sub ("Montserrat", replacement="Caribbean", clist$region)
clist$region <- sub ("Puerto Rico", replacement="Caribbean", clist$region)
clist$region <- sub ("Saint-Barth??lemy", replacement="Caribbean", clist$region)
clist$region <- sub ("Saint Kitts and Nevis", replacement="Caribbean", clist$region)
clist$region <- sub ("Saint Lucia", replacement="Caribbean", clist$region)
clist$region <- sub ("Saint Martin (French part)", replacement="Caribbean", clist$region)
clist$region <- sub ("Saint Vincent and the Grenadines", replacement="Caribbean", clist$region)
clist$region <- sub ("Sint Maarten (Dutch part)", replacement="Caribbean", clist$region)
clist$region <- sub ("Trinidad and Tobago", replacement="Caribbean", clist$region)
clist$region <- sub ("Turks and Caicos Islands", replacement="Caribbean", clist$region)
clist$region <- sub ("United States Virgin Islands", replacement="Caribbean", clist$region)
clist$region <- sub ("Dominica", replacement="Caribbean", clist$region)

clist$region <- sub ("Belize", replacement="Central America", clist$region)
clist$region <- sub ("Costa Rica", replacement="Central America", clist$region)
clist$region <- sub ("El Salvador", replacement="Central America", clist$region)
clist$region <- sub ("Guatemala", replacement="Central America", clist$region)
clist$region <- sub ("Honduras", replacement="Central America", clist$region)
clist$region <- sub ("Mexico", replacement="Central America", clist$region)
clist$region <- sub ("Nicaragua", replacement="Central America", clist$region)
clist$region <- sub ("Panama", replacement="Central America", clist$region)

clist$region <- sub ("Argentina", replacement="South America", clist$region)
clist$region <- sub ("Bolivia", replacement="South America", clist$region)
clist$region <- sub ("Brazil", replacement="South America", clist$region)
clist$region <- sub ("Chile", replacement="South America", clist$region)
clist$region <- sub ("Colombia", replacement="South America", clist$region)
clist$region <- sub ("Ecuador", replacement="South America", clist$region)
clist$region <- sub ("Falkland Islands (Malvinas)", replacement="South America", clist$region)
clist$region <- sub ("French Guiana", replacement="South America", clist$region)
clist$region <- sub ("Guyana", replacement="South America", clist$region)
clist$region <- sub ("Paraguay", replacement="South America", clist$region)
clist$region <- sub ("Peru", replacement="South America", clist$region)
clist$region <- sub ("Suriname", replacement="South America", clist$region)
clist$region <- sub ("Uruguay", replacement="South America", clist$region)
clist$region <- sub ("Venezuela", replacement="South America", clist$region)

clist$region <- sub ("Bermuda", replacement="North America", clist$region)
clist$region <- sub ("Canada", replacement="North America", clist$region)
clist$region <- sub ("Greenland", replacement="North America", clist$region)
clist$region <- sub ("Saint Pierre and Miquelon", replacement="North America", clist$region)
clist$region <- sub ("United States", replacement="North America", clist$region)

clist$region <- sub ("Kazakhstan", replacement="Central Asia", clist$region)
clist$region <- sub ("Kyrgyzstan", replacement="Central Asia", clist$region)
clist$region <- sub ("Tajikistan", replacement="Central Asia", clist$region)
clist$region <- sub ("Turkmenistan", replacement="Central Asia", clist$region)
clist$region <- sub ("Uzbekistan", replacement="Central Asia", clist$region)

clist$region <- sub ("China", replacement="East Asia", clist$region)
clist$region <- sub ("China, Hong Kong Special Administrative Region", replacement="East Asia", clist$region)
clist$region <- sub ("China, Macao Special Administrative Region", replacement="East Asia", clist$region)
clist$region <- sub ("North Korea", replacement="East Asia", clist$region)
clist$region <- sub ("Japan", replacement="East Asia", clist$region)
clist$region <- sub ("Mongolia", replacement="East Asia", clist$region)
clist$region <- sub ("South Korea", replacement="East Asia", clist$region)
clist$region <- sub ("Taiwan", replacement="East Asia", clist$region)

clist$region <- sub ("Afghanistan", replacement="South Asia", clist$region)
clist$region <- sub ("Bangladesh", replacement="South Asia", clist$region)
clist$region <- sub ("Bhutan", replacement="South Asia", clist$region)
clist$region <- sub ("India", replacement="South Asia", clist$region)
clist$region <- sub ("Iran", replacement="South Asia", clist$region)
clist$region <- sub ("Maldives", replacement="South Asia", clist$region)
clist$region <- sub ("Nepal", replacement="South Asia", clist$region)
clist$region <- sub ("Pakistan", replacement="South Asia", clist$region)
clist$region <- sub ("Sri Lanka", replacement="South Asia", clist$region)

clist$region <- sub ("Brunei", replacement="South-East Asia", clist$region)
clist$region <- sub ("Cambodia", replacement="South-East Asia", clist$region)
clist$region <- sub ("Indonesia", replacement="South-East Asia", clist$region)
clist$region <- sub ("Laos", replacement="South-East Asia", clist$region)
clist$region <- sub ("Malaysia", replacement="South-East Asia", clist$region)
clist$region <- sub ("Myanmar/Burma", replacement="South-East Asia", clist$region)
clist$region <- sub ("Philippines", replacement="South-East Asia", clist$region)
clist$region <- sub ("Singapore", replacement="South-East Asia", clist$region)
clist$region <- sub ("Thailand", replacement="South-East Asia", clist$region)
clist$region <- sub ("Timor Leste", replacement="South-East Asia", clist$region)
clist$region <- sub ("Vietnam", replacement="South-East Asia", clist$region)

clist$region <- sub ("Armenia", replacement="West Asia", clist$region)
clist$region <- sub ("Azerbaijan", replacement="West Asia", clist$region)
clist$region <- sub ("Bahrain", replacement="West Asia", clist$region)
clist$region <- sub ("Cyprus", replacement="West Asia", clist$region)
clist$region <- sub ("Georgia", replacement="West Asia", clist$region)
clist$region <- sub ("Iraq", replacement="West Asia", clist$region)
clist$region <- sub ("Israel", replacement="West Asia", clist$region)
clist$region <- sub ("Jordan", replacement="West Asia", clist$region)
clist$region <- sub ("Kuwait", replacement="West Asia", clist$region)
clist$region <- sub ("Lebanon", replacement="West Asia", clist$region)
clist$region <- sub ("Oman", replacement="West Asia", clist$region)
clist$region <- sub ("Qatar", replacement="West Asia", clist$region)
clist$region <- sub ("Saudi Arabia", replacement="West Asia", clist$region)
clist$region <- sub ("State of Palestine", replacement="West Asia", clist$region)
clist$region <- sub ("Syria", replacement="West Asia", clist$region)
clist$region <- sub ("Turkey", replacement="West Asia", clist$region)
clist$region <- sub ("United Arab Emirates", replacement="West Asia", clist$region)
clist$region <- sub ("Yemen Arab Republic", replacement="West Asia", clist$region)
clist$region <- sub ("Yemen People's Republic", replacement="West Asia", clist$region)
clist$region <- sub ("Yemen", replacement="West Asia", clist$region)

clist$region <- sub ("Belarus", replacement="East Europe", clist$region)
clist$region <- sub ("Bulgaria", replacement="East Europe", clist$region)
clist$region <- sub ("Czech Republic", replacement="East Europe", clist$region)
clist$region <- sub ("Czechoslovakia", replacement="East Europe", clist$region)
clist$region <- sub ("East Germany", replacement="East Europe", clist$region)
clist$region <- sub ("Hungary", replacement="East Europe", clist$region)
clist$region <- sub ("Poland", replacement="East Europe", clist$region)
clist$region <- sub ("Moldova", replacement="East Europe", clist$region)
clist$region <- sub ("Romania", replacement="East Europe", clist$region)
clist$region <- sub ("Russia", replacement="East Europe", clist$region)
clist$region <- sub ("Slovakia", replacement="East Europe", clist$region)
clist$region <- sub ("Soviet Union", replacement="East Europe", clist$region)
clist$region <- sub ("Ukraine", replacement="East Europe", clist$region)

clist$region <- sub ("??land Islands", replacement="North Europe", clist$region)
clist$region <- sub ("Channel Islands", replacement="North Europe", clist$region)
clist$region <- sub ("Denmark", replacement="North Europe", clist$region)
clist$region <- sub ("Estonia", replacement="North Europe", clist$region)
clist$region <- sub ("Faeroe Islands", replacement="North Europe", clist$region)
clist$region <- sub ("Finland", replacement="North Europe", clist$region)
clist$region <- sub ("Guernsey", replacement="North Europe", clist$region)
clist$region <- sub ("Iceland", replacement="North Europe", clist$region)
clist$region <- sub ("Ireland", replacement="North Europe", clist$region)
clist$region <- sub ("Isle of Man", replacement="North Europe", clist$region)
clist$region <- sub ("Jersey", replacement="North Europe", clist$region)
clist$region <- sub ("Latvia", replacement="North Europe", clist$region)
clist$region <- sub ("Lithuania", replacement="North Europe", clist$region)
clist$region <- sub ("Norway", replacement="North Europe", clist$region)
clist$region <- sub ("Sark", replacement="North Europe", clist$region)
clist$region <- sub ("Svalbard and Jan Mayen Islands", replacement="North Europe", clist$region)
clist$region <- sub ("Sweden", replacement="North Europe", clist$region)
clist$region <- sub ("United Kingdom", replacement="North Europe", clist$region)

clist$region <- sub ("Albania", replacement="South Europe", clist$region)
clist$region <- sub ("Andorra", replacement="South Europe", clist$region)
clist$region <- sub ("Bosnia and Herzegovina", replacement="South Europe", clist$region)
clist$region <- sub ("Croatia", replacement="South Europe", clist$region)
clist$region <- sub ("Gibraltar", replacement="South Europe", clist$region)
clist$region <- sub ("Greece", replacement="South Europe", clist$region)
clist$region <- sub ("Holy See", replacement="South Europe", clist$region)
clist$region <- sub ("Italy", replacement="South Europe", clist$region)
clist$region <- sub ("Kosovo", replacement="South Europe", clist$region)
clist$region <- sub ("Malta", replacement="South Europe", clist$region)
clist$region <- sub ("Portugal", replacement="South Europe", clist$region)
clist$region <- sub ("San Marino", replacement="South Europe", clist$region)
clist$region <- sub ("Serbia and Montenegro", replacement="South Europe", clist$region)
clist$region <- sub ("Slovenia", replacement="South Europe", clist$region)
clist$region <- sub ("Spain", replacement="South Europe", clist$region)
clist$region <- sub ("Macedonia", replacement="South Europe", clist$region)
clist$region <- sub ("Montenegro", replacement="South Europe", clist$region)
clist$region <- sub ("Serbia", replacement="South Europe", clist$region)
clist$region <- sub ("Yugoslavia", replacement="South Europe", clist$region)

clist$region <- sub ("Austria", replacement="West Europe", clist$region)
clist$region <- sub ("Belgium", replacement="West Europe", clist$region)
clist$region <- sub ("France", replacement="West Europe", clist$region)
clist$region <- sub ("Germany Federal Republic", replacement="West Europe", clist$region)
clist$region <- sub ("Germany", replacement="West Europe", clist$region)
clist$region <- sub ("Liechtenstein", replacement="West Europe", clist$region)
clist$region <- sub ("Luxembourg", replacement="West Europe", clist$region)
clist$region <- sub ("Monaco", replacement="West Europe", clist$region)
clist$region <- sub ("Netherlands", replacement="West Europe", clist$region)
clist$region <- sub ("Switzerland", replacement="West Europe", clist$region)

clist$region <- sub ("Australia", replacement="ANZ", clist$region)
clist$region <- sub ("New Zealand", replacement="ANZ", clist$region)
clist$region <- sub ("Norfolk Island", replacement="ANZ", clist$region)
clist$region <- sub ("ANZ", replacement="Australia and New Zealand", clist$region)

clist$region <- sub ("Fiji", replacement="Melanesia", clist$region)
clist$region <- sub ("New Caledonia", replacement="Melanesia", clist$region)
clist$region <- sub ("Papua New Guinea", replacement="Melanesia", clist$region)
clist$region <- sub ("Solomon Is.", replacement="Melanesia", clist$region)
clist$region <- sub ("Vanuatu", replacement="Melanesia", clist$region)

clist$region <- sub ("Guam", replacement="Micronesia", clist$region)
clist$region <- sub ("Kiribati", replacement="Micronesia", clist$region)
clist$region <- sub ("Marshall Islands", replacement="Micronesia", clist$region)
clist$region <- sub ("Micronesia (Federated States of)", replacement="Micronesia", clist$region)
clist$region <- sub ("Nauru", replacement="Micronesia", clist$region)
clist$region <- sub ("Northern Mariana Islands", replacement="Micronesia", clist$region)
clist$region <- sub ("Palau", replacement="Micronesia", clist$region)

clist$region <- sub ("American Samoa", replacement="Polynesia", clist$region)
clist$region <- sub ("Cook Islands", replacement="Polynesia", clist$region)
clist$region <- sub ("French Polynesia", replacement="Polynesia", clist$region)
clist$region <- sub ("Niue", replacement="Polynesia", clist$region)
clist$region <- sub ("Pitcairn", replacement="Polynesia", clist$region)
clist$region <- sub ("Samoa", replacement="Polynesia", clist$region)
clist$region <- sub ("Tokelau", replacement="Polynesia", clist$region)
clist$region <- sub ("Tonga", replacement="Polynesia", clist$region)
clist$region <- sub ("Tuvalu", replacement="Polynesia", clist$region)
clist$region <- sub ("Wallis and Futuna Islands", replacement="Polynesia", clist$region)

clist$region <- sub ("Guinea", replacement="West Africa", clist$region)

clist$region <- factor (clist$region)
sort (unique (clist$region))  ## there are 20 regions; the UN list has 21, but there is no country from Micronesia or Polynesia in the sample


## (1.1.3) overlap between neighbor measures (fn. 9)

# getting rid of the NA's in the distance dummies
clist$region2 <- clist$region
distances <- merge (distances, clist[,c ("country", "region")], by.x="country", by.y="country", all.x=T, all.y=T)
distances <- merge (distances, clist[,c ("country", "region2")], by.x="country2", by.y="country", all.x=T, all.y=T)
distances$d50b <- with (distances, ifelse (is.na (d50), 0, d50))
distances$d800b <- with (distances, ifelse (is.na (d800), 0, d800))
distances$dUN <- with (distances, ifelse (region == region2, 1, 0))
clist <- NULL

# measuring overlap
overl <- distances[distances$date=="2011-07-09",c ("country", "country2", "d50", "d800", "dUN")]
overl$country <- factor (overl$country)
overl$country2 <- factor (overl$country2)

mean (overl[!is.na (overl$d50) & overl$d50==1,]$d800); mean (overl[!is.na (overl$d50) & overl$d50==1,]$dUN)
# [1] 1
# [1] 0.6962
mean (overl[!is.na (overl$d800) & overl$d800==1,]$d50); mean (overl[!is.na (overl$d800) & overl$d800==1,]$dUN)
# [1] 0.3275
# [1] 0.5034
mean (overl[!is.na (overl$dUN) & !is.na (overl$d50) & overl$dUN==1,]$d50); mean (overl[!is.na (overl$dUN) & !is.na (overl$d50) &  overl$dUN==1,]$d800)
# [1] 0.2688
# [1] 0.5934


## (1.1.4) number of protests in GDELT, SPEED and MMD data (fn. 27)
sum (gdelt$pbeg_gdelt)
sum (speed$pbegSpeed)
sum (mmd$pbegMMD)


## (1.1.5) Wald Tests mentioned in p. 30

# (a) No election

# protest success
mod01 <- plm (pbega2 ~ n50_d45_wSuc_any2, data=bCont.cy, effect="individual", model="within", index="countryYear")
mod02 <- plm (pbega2 ~ n50_d90_wSuc_any2, data=bCont.cy, effect="individual", model="within", index="countryYear")
mod03 <- plm (pbega2 ~ n50_d120_wSuc_any2, data=bCont.cy, effect="individual", model="within", index="countryYear")

mod04 <- plm (pbega2 ~ n800_d45_wSuc_any2, data=bN800.cy, effect="individual", model="within", index="countryYear")
mod05 <- plm (pbega2 ~ n800_d90_wSuc_any2, data=bN800.cy, effect="individual", model="within", index="countryYear")
mod06 <- plm (pbega2 ~ n800_d120_wSuc_any2, data=bN800.cy, effect="individual", model="within", index="countryYear")

# government repression
mod07 <- plm (pbega2 ~ n50_d45_wRepr_any2, data=bCont.cy, effect="individual", model="within", index="countryYear")
mod08 <- plm (pbega2 ~ n50_d90_wRepr_any2, data=bCont.cy, effect="individual", model="within", index="countryYear")
mod09 <- plm (pbega2 ~ n50_d120_wRepr_any2, data=bCont.cy, effect="individual", model="within", index="countryYear")

mod10 <- plm (pbega2 ~ n800_d45_wRepr_any2, data=bN800.cy, effect="individual", model="within", index="countryYear")
mod11 <- plm (pbega2 ~ n800_d90_wRepr_any2, data=bN800.cy, effect="individual", model="within", index="countryYear")
mod12 <- plm (pbega2 ~ n800_d120_wRepr_any2, data=bN800.cy, effect="individual", model="within", index="countryYear")

# (b) Election within the last 30 days

# protest success
mod13 <- plm (pbega2 ~ n50_d45_wSuc_any2 + ele30, data=bCont.cy, effect="individual", model="within", index="countryYear")
mod14 <- plm (pbega2 ~ n50_d90_wSuc_any2 + ele30, data=bCont.cy, effect="individual", model="within", index="countryYear")
mod15 <- plm (pbega2 ~ n50_d120_wSuc_any2 + ele30, data=bCont.cy, effect="individual", model="within", index="countryYear")

mod16 <- plm (pbega2 ~ n800_d45_wSuc_any2 + ele30, data=bN800.cy, effect="individual", model="within", index="countryYear")
mod17 <- plm (pbega2 ~ n800_d90_wSuc_any2 + ele30, data=bN800.cy, effect="individual", model="within", index="countryYear")
mod18 <- plm (pbega2 ~ n800_d120_wSuc_any2 + ele30, data=bN800.cy, effect="individual", model="within", index="countryYear")

# government repression
mod19 <- plm (pbega2 ~ n50_d45_wRepr_any2 + ele30, data=bCont.cy, effect="individual", model="within", index="countryYear")
mod20 <- plm (pbega2 ~ n50_d90_wRepr_any2 + ele30, data=bCont.cy, effect="individual", model="within", index="countryYear")
mod21 <- plm (pbega2 ~ n50_d120_wRepr_any2 + ele30, data=bCont.cy, effect="individual", model="within", index="countryYear")

mod22 <- plm (pbega2 ~ n800_d45_wRepr_any2 + ele30, data=bN800.cy, effect="individual", model="within", index="countryYear")
mod23 <- plm (pbega2 ~ n800_d90_wRepr_any2 + ele30, data=bN800.cy, effect="individual", model="within", index="countryYear")
mod24 <- plm (pbega2 ~ n800_d120_wRepr_any2 + ele30, data=bN800.cy, effect="individual", model="within", index="countryYear")

wtests <- list ()

# (a) No election

# protest success
wtests[[1]] <- linearHypothesis (mod01, c ("n50_d45_wSuc_any2option2-all successful = 0", "n50_d45_wSuc_any2option3-some successful = 0", "n50_d45_wSuc_any2option4-none successful = 0"))
wtests[[2]] <- linearHypothesis (mod02, c ("n50_d90_wSuc_any2option2-all successful = 0", "n50_d90_wSuc_any2option3-some successful = 0", "n50_d90_wSuc_any2option4-none successful = 0"))
wtests[[3]] <- linearHypothesis (mod03, c ("n50_d120_wSuc_any2option2-all successful = 0", "n50_d120_wSuc_any2option3-some successful = 0", "n50_d120_wSuc_any2option4-none successful = 0"))
wtests[[4]] <- linearHypothesis (mod04, c ("n800_d45_wSuc_any2option2-all successful = 0", "n800_d45_wSuc_any2option3-some successful = 0", "n800_d45_wSuc_any2option4-none successful = 0"))
wtests[[5]] <- linearHypothesis (mod05, c ("n800_d90_wSuc_any2option2-all successful = 0", "n800_d90_wSuc_any2option3-some successful = 0", "n800_d90_wSuc_any2option4-none successful = 0"))
wtests[[6]] <- linearHypothesis (mod06, c ("n800_d120_wSuc_any2option2-all successful = 0", "n800_d120_wSuc_any2option3-some successful = 0", "n800_d120_wSuc_any2option4-none successful = 0"))

# government repression
wtests[[7]] <- linearHypothesis (mod07, c ("n50_d45_wRepr_any2option2-all repressed = 0", "n50_d45_wRepr_any2option3-some repressed = 0", "n50_d45_wRepr_any2option4-none repressed = 0"))
wtests[[8]] <- linearHypothesis (mod08, c ("n50_d90_wRepr_any2option2-all repressed = 0", "n50_d90_wRepr_any2option3-some repressed = 0", "n50_d90_wRepr_any2option4-none repressed = 0"))
wtests[[9]] <- linearHypothesis (mod09, c ("n50_d120_wRepr_any2option2-all repressed = 0", "n50_d120_wRepr_any2option3-some repressed = 0", "n50_d120_wRepr_any2option4-none repressed = 0"))
wtests[[10]] <- linearHypothesis (mod10, c ("n800_d45_wRepr_any2option2-all repressed = 0", "n800_d45_wRepr_any2option3-some repressed = 0", "n800_d45_wRepr_any2option4-none repressed = 0"))
wtests[[11]] <- linearHypothesis (mod11, c ("n800_d90_wRepr_any2option2-all repressed = 0", "n800_d90_wRepr_any2option3-some repressed = 0", "n800_d90_wRepr_any2option4-none repressed = 0"))
wtests[[12]] <- linearHypothesis (mod12, c ("n800_d120_wRepr_any2option2-all repressed = 0", "n800_d120_wRepr_any2option3-some repressed = 0", "n800_d120_wRepr_any2option4-none repressed = 0"))

# (b) Election within the last 30 days

# protest success
wtests[[13]] <- linearHypothesis (mod13, c ("n50_d45_wSuc_any2option2-all successful = 0", "n50_d45_wSuc_any2option3-some successful = 0", "n50_d45_wSuc_any2option4-none successful = 0"))
wtests[[14]] <- linearHypothesis (mod14, c ("n50_d90_wSuc_any2option2-all successful = 0", "n50_d90_wSuc_any2option3-some successful = 0", "n50_d90_wSuc_any2option4-none successful = 0"))
wtests[[15]] <- linearHypothesis (mod15, c ("n50_d120_wSuc_any2option2-all successful = 0", "n50_d120_wSuc_any2option3-some successful = 0", "n50_d120_wSuc_any2option4-none successful = 0"))
wtests[[16]] <- linearHypothesis (mod16, c ("n800_d45_wSuc_any2option2-all successful = 0", "n800_d45_wSuc_any2option3-some successful = 0", "n800_d45_wSuc_any2option4-none successful = 0"))
wtests[[17]] <- linearHypothesis (mod17, c ("n800_d90_wSuc_any2option2-all successful = 0", "n800_d90_wSuc_any2option3-some successful = 0", "n800_d90_wSuc_any2option4-none successful = 0"))
wtests[[18]] <- linearHypothesis (mod18, c ("n800_d120_wSuc_any2option2-all successful = 0", "n800_d120_wSuc_any2option3-some successful = 0", "n800_d120_wSuc_any2option4-none successful = 0"))

# government repression
wtests[[19]] <- linearHypothesis (mod19, c ("n50_d45_wRepr_any2option2-all repressed = 0", "n50_d45_wRepr_any2option3-some repressed = 0", "n50_d45_wRepr_any2option4-none repressed = 0"))
wtests[[20]] <- linearHypothesis (mod20, c ("n50_d90_wRepr_any2option2-all repressed = 0", "n50_d90_wRepr_any2option3-some repressed = 0", "n50_d90_wRepr_any2option4-none repressed = 0"))
wtests[[21]] <- linearHypothesis (mod21, c ("n50_d120_wRepr_any2option2-all repressed = 0", "n50_d120_wRepr_any2option3-some repressed = 0", "n50_d120_wRepr_any2option4-none repressed = 0"))
wtests[[22]] <- linearHypothesis (mod22, c ("n800_d45_wRepr_any2option2-all repressed = 0", "n800_d45_wRepr_any2option3-some repressed = 0", "n800_d45_wRepr_any2option4-none repressed = 0"))
wtests[[23]] <- linearHypothesis (mod23, c ("n800_d90_wRepr_any2option2-all repressed = 0", "n800_d90_wRepr_any2option3-some repressed = 0", "n800_d90_wRepr_any2option4-none repressed = 0"))
wtests[[24]] <- linearHypothesis (mod24, c ("n800_d120_wRepr_any2option2-all repressed = 0", "n800_d120_wRepr_any2option3-some repressed = 0", "n800_d120_wRepr_any2option4-none repressed = 0"))


### (1.2) Table 2: Data Overview

tab.all <- rbind (
  cbind (rbind (tabCont30[2,], tabCont45[2,], tabCont90[2,], tabCont120[2,])
         , rbind (tabN800.30[2,], tabN800.45[2,], tabN800.90[2,], tabN800.120[2,])
         , rbind (tabUNReg30[2,], tabUNReg45[2,], tabUNReg90[2,], tabUNReg120[2,]))
  , cbind (rbind (tabCont30abs[2,], tabCont45abs[2,], tabCont90abs[2,], tabCont120abs[2,])
           , rbind (tabN800.30abs[2,], tabN800.45abs[2,], tabN800.90abs[2,], tabN800.120abs[2,])
           , rbind (tabUNReg30abs[2,], tabUNReg45abs[2,], tabUNReg90abs[2,], tabUNReg120abs[2,])))
tab.all2 <- as.data.frame (rbind (
  matrix (paste (sprintf ("%.3f", round (tab.all[1:4,], 3)), "\\%", sep=""), ncol=6)
  , matrix (paste ("[", sprintf ("%.0f", round (tab.all[5:8,], 0)), "]", sep=""), ncol=6)))

# adding the asterisks to indicate statistically significant differences
tab.all2$V2 <- as.character (tab.all2$V2)
tab.all2$V4 <- as.character (tab.all2$V4)
tab.all2$V6 <- as.character (tab.all2$V6)
cbind (c (tvalCont30$p.value, tvalCont45$p.value, tvalCont90$p.value, tvalCont120$p.value)
       , c (tvalN800.30$p.value, tvalN800.45$p.value, tvalN800.90$p.value, tvalN800.120$p.value)
       , c (tvalUNReg30$p.value, tvalUNReg45$p.value, tvalUNReg90$p.value, tvalUNReg120$p.value))<0.05 ## three cases
tab.all2[3,2] <- paste (tab.all2[3,2], "*", sep="")
tab.all2[3:4,4] <- paste (tab.all2[3:4,4], "*", sep="")

odd <- seq (1, nrow (tab.all), by=2); even <- seq (2, nrow (tab.all), by=2)
row.names (tab.all2) <- as.numeric (c (odd, even))
tab.all2 <- as.matrix (tab.all2[order (as.numeric (row.names (tab.all2))),])
(tab.all3 <- cbind (
  tab.all2[,1:2]
  , rep ("", nrow(tab.all))
  , tab.all2[,3:4]
  , rep ("", nrow(tab.all))
  , tab.all2[,5:6]))

rows <- c ("\\textbf{30 days}", "", "\\textbf{45 days}", "", "\\textbf{90 days}", "", "\\textbf{120 days}", "")

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\textbf{$50$km}} & & \\multicolumn{2}{c}{\\textbf{$800$km}} & & \\multicolumn{2}{c}{\\textbf{UN region}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} \\\\ \\cmidrule{2-3} \\cmidrule{5-6} \\cmidrule{8-9} \n")
Header3 <- paste ("\\multicolumn{1}{l}{} & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} \\\\ \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{protest in last...}} & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$command <- c (Header1, Header2, Header3, Header4)
print (xtable (cbind (rows, tab.all3)
               , align=c("c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Data Overview"
               , label="T:overview")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (1.3) Figure 1: Protest Diffusion Video Time-lapse Vide Snapshots, 1989-2011

## (1.3.1) Processing the data

# changing the country names
cshap <- cshp ()
summary (sort (unique (base$country)) %in% sort (unique (cshap$CNTRY_NAME)))  ## 6 discrepancies
sort (unique (base$country))[(sort (unique (base$country)) %in% sort (unique (cshap$CNTRY_NAME)))==F]

base$country <- as.character (base$country)
base[base$country=="Congo, Democratic Republic of",]$country <- "Congo, DRC"
base[base$country=="Congo, Republic of",]$country <- "Congo"
base[base$country=="East Germany",]$country <- "Germany Democratic Republic"
base[base$country=="Ivory Coast",]$country <- "Cote d'Ivoire"
base[base$country=="Myanmar/Burma",]$country <- "Myanmar"
base[base$country=="Soviet Union",]$country <- "USSR"

summary (sort (unique (base$country)) %in% sort (unique (cshap$CNTRY_NAME)))  ## No more discrepancies. Good

# selecting colors for UN regions
regs <- as.data.frame (sort (unique (base$region)))
colnames (regs) <- "region"
regs$regColor <- rep (c ("#8dd3c7", "#ffffb3", "#bebada", "#fb8072", "#80b1d3", "#fdb462", "#b3de69", "#fccde5", "#d9d9d9", "#bc80bd"), 2)

# merging and keeping the variables of interest only
bmap <- merge (base, regs, by="region")

# keeping the variables of interest only
bmap <- bmap[,c ("country", "date", "region", "regColor", "polity", "autocracy", "pbega", "inplace", "protend90", "size", "n50_d90_wSize_any", "nUN_d90_wequal_any")]
bmap$country <- factor (bmap$country)
bmap$inplace <- with (bmap, ifelse (pbega == 1 | inplace==1, 1, 0))
bmap$size <- with (bmap, ifelse (inplace==1 | protend90==1, size, 0))
bmap$size2 <- sqrt (bmap$size) ## for plotting; we want smaller dots
bmap$n50_d90_wSize_any2 <- sqrt (bmap$n50_d90_wSize_any) ## ditto


## Getting the weekly averages/maxima/minima

# creating week IDs
weeks <- as.data.frame (sort (unique (bmap$date)))
colnames (weeks) <- "date"
weeks$week <- sort (rep (1:(length (unique (bmap$date))/7), 7))
bmap <- merge (bmap, weeks, by="date")

# calculating the average/maxima for every country-week, and merging
bmap$id <- with (bmap, factor (paste (country, week, sep="_")))

bmap.w <- as.data.frame (with (bmap, factor (names (by (inplace, id, max, na.rm=T)))))
colnames (bmap.w) <- "id"
bmap.w$inplace <- with (bmap, by (inplace, id, max, na.rm=T))[]
bmap.w$protend90 <- with (bmap, by (protend90, id, max, na.rm=T))[]
bmap.w$size <- with (bmap, by (size, id, max, na.rm=T))[]
bmap.w$nUN_d90_wequal_any <- with (bmap, by (nUN_d90_wequal_any, id, max, na.rm=T))[]
bmap.w$size2 <- with (bmap, by (size2, id, max, na.rm=T))[]

# merging
bmap$inplace <- NULL
bmap$protend90 <- NULL
bmap$size <- NULL
bmap$nUN_d90_wequal_any <- NULL
bmap$size2 <- NULL
bmap <- merge (bmap, bmap.w, by="id", all.x=T, all.y=T)

# keeping every seventh day only
bmap7th <- bmap[which (bmap$date %in% as.Date ((as.Date ("1989-01-01"):as.Date ("2011-12-31"))[seq (1, 8400, by=7)], origin="1970-01-1")),]
bmap7th$country <- factor (bmap7th$country)
bmap7th$region <- factor (bmap7th$region)
bmap7th$id <- factor (bmap7th$id)
summary (bmap7th); dim (bmap7th)


## (1.3.2) Creating the maps

# downloading the map for every day of interest
maps <- list ()
for (d in 1:length (unique (bmap7th$date))){
  maps[[d]] <- cshp (date=as.Date (sort (unique (bmap7th$date))[d]))
} ## warnings reported. Don't worry
length (maps)

# creating the dataset with centroids
cents <- list ()
for (d in 1:length (maps)){
  cents[[d]] <- SpatialPointsDataFrame (coords=coordinates (maps[[d]]), data=as.data.frame (maps[[d]]))
}
length (cents)


## (1.3.3) Creating the frames for the full video

## setting graphical parameters
densDemo <- 70
densAutoc <- 30
cex.text <- 2.25
protCol <- "#d01c8b"
protColsingle <- "#d01c8b"
protNeigh <- "#f1b6da"
protNeighsingle <- "#f1b6da"
colSingle <- "#2ca25f"
protShape <- 21
lwdt <- 0.75

plotWidth <- 1400
aspect.ratio <- 2
par (mar = c (0.25, 0.25, 1, 0.25))

## running the loop

# (a) protests in sending country only
system.time (
  #for (d in 1:length (unique (bmap7th$date))){
  for (d in 1:20){
    
    ## building the data frames corresponding to that day
    mapTmp <- merge (maps[[d]], bmap7th[bmap7th$date==sort (unique (bmap7th$date))[d],], by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)
    centTmp <- merge (cents[[d]], bmap7th[bmap7th$date==sort (unique (bmap7th$date))[d],], by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)
    
    fileName <- paste ("prot_", sort (unique (bmap7th$date))[d], ".png", sep="")
    png (file.path ("Figures/Video frames", fileName), w=plotWidth, h=plotWidth/aspect.ratio)
    
    ## drawing the plots
    plot (mapTmp, col=mapTmp$regColor, density=ifelse (is.na (mapTmp$autocracy), 0, ifelse (mapTmp$autocracy==1, densAutoc, densDemo)), lwd=lwdt)
    text (x=0, y=87, paste (unique (format (mapTmp$date, "%Y")), "\n", unique (format (mapTmp$date, "%B")), sep=""), cex=cex.text)
    points (centTmp, pch=ifelse (centTmp$inplace==0, NA, protShape), cex=centTmp$size2, bg=protCol)
    points (centTmp, pch=ifelse (centTmp$protend90==0, NA, protShape), cex=centTmp$size2, bg=NA, col=protNeigh, lwd=lwdt*4)
    legend (
      "bottomleft"
      , bty="n"
      , density=c (densDemo, densAutoc)
      , legend=c ("Democracy", "Autocracy")
      , cex=cex.text*2/3
    )
    legend (
      x=-192.5, y=-15
      , bty="n"
      , legend=c ("Size: < 1,000", "Size: 1,000-10,000", "Size: 10,000-100,000", "Size: 100,000-1 million", "Size: 1 million or more")
      , pch=19
      , col=protCol
      , pt.cex=sqrt (1:5)
      , cex=cex.text*2/3
    )
    dev.off ()
  }
)

# (b) Single color version
system.time (
  for (d in 1:length (unique (bmap7th$date))){
    
    ## building the data frames corresponding to that day
    mapTmp <- merge (maps[[d]], bmap7th[bmap7th$date==sort (unique (bmap7th$date))[d],], by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)
    centTmp <- merge (cents[[d]], bmap7th[bmap7th$date==sort (unique (bmap7th$date))[d],], by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)
    
    fileName <- paste ("protBW_", sort (unique (bmap7th$date))[d], ".png", sep="")
    png (file.path ("Figures/Video frames", fileName), w=plotWidth, h=plotWidth/aspect.ratio)
    
    ## drawing the plots
    plot (mapTmp, col=colSingle, density=ifelse (is.na (mapTmp$autocracy), 0, ifelse (mapTmp$autocracy==1, densAutoc, densDemo)), lwd=lwdt)
    text (x=0, y=87, paste (unique (format (mapTmp$date, "%Y")), "\n", unique (format (mapTmp$date, "%B")), sep=""), cex=cex.text)
    points (centTmp, pch=ifelse (centTmp$inplace==0, NA, protShape), cex=centTmp$size2, bg=protColsingle)
    points (centTmp, pch=ifelse (centTmp$protend90==0, NA, protShape), cex=centTmp$size2, bg=NA, col=protNeighsingle, lwd=lwdt*4)
    legend (
      "bottomleft"
      , bty="n"
      , density=c (densDemo, densAutoc)
      , fill=colSingle
      , legend=c ("Democracy", "Autocracy")
      , cex=cex.text*2/3
    )
    legend (
      x=-192.5, y=-15
      , bty="n"
      , legend=c ("Size: < 1,000", "Size: 1,000-10,000", "Size: 10,000-100,000", "Size: 100,000-1 million", "Size: 1 million or more")
      , pch=19
      , col=protColsingle
      , pt.cex=sqrt (1:5)
      , cex=cex.text*2/3
    )
    dev.off ()
  }
) ## 37689 seconds


## (1.3.4) Creating the snapshots for submission: calculating the annual averages for 1989 and 2011

# creating week IDs
bmap$year <- as.numeric (with (bmap, format (as.Date (date),"%Y")))
bmap$y89 <- with (bmap, ifelse (year==1989, 1, 0))
bmap$y92 <- with (bmap, ifelse (year==1992, 1, 0))
bmap$y8992 <- with (bmap, ifelse (year==1989 | year==1990 | year==1991 | year==1992, 1, 0))
bmap$y11 <- with (bmap, ifelse (year==2011, 1, 0))

# calculating the average/maxima for every country-year, and merging
bmap$id2 <- with (bmap, factor (paste (country, year, sep="_")))

bmap.y <- as.data.frame (with (bmap, factor (names (by (inplace, id2, max, na.rm=T)))))
colnames (bmap.y) <- "id"
bmap.y$country <- with (bmap.y, factor (unlist (strsplit (as.character (id), split="_"))))[seq (1, 2*nrow (bmap.y), by=2)]
bmap.y$y89 <- with (bmap, by (y89, id2, max, na.rm=T))[]
bmap.y$y92 <- with (bmap, by (y92, id2, max, na.rm=T))[]
bmap.y$y8992 <- with (bmap, by (y8992, id2, max, na.rm=T))[]
bmap.y$y11 <- with (bmap, by (y11, id2, max, na.rm=T))[]
bmap.y$inplace <- with (bmap, by (inplace, id2, max, na.rm=T))[]
bmap.y$size <- with (bmap, by (size, id2, max, na.rm=T))[]
bmap.y$size2 <- with (bmap, by (size2, id2, max, na.rm=T))[]
bmap.y$nUN_d90_wequal_any <- with (bmap, by (nUN_d90_wequal_any, id2, max.na))[]
bmap.y$n50_d90_wSize_any <- with (bmap, by (n50_d90_wSize_any, id2, max.na))[]
bmap.y$n50_d90_wSize_any2 <- with (bmap, by (n50_d90_wSize_any2, id2, max.na))[]
bmap.y$autocracy <- with (bmap, by (autocracy, id2, unique, na.rm=T))[] ## a single value for the entire year

# 1989-1992 period
bmap.y8992 <- bmap[bmap$y8992==1,]
bmap.y8992b <- as.data.frame (with (bmap.y8992, factor (names (by (inplace, country, max, na.rm=T)))))
colnames (bmap.y8992b) <- "country"
bmap.y8992b$inplace <- with (bmap.y8992, by (inplace, country, max, na.rm=T))[]
bmap.y8992b$size <- with (bmap.y8992, by (size, country, max, na.rm=T))[]
bmap.y8992b$size2 <- with (bmap.y8992, by (size2, country, max, na.rm=T))[]
bmap.y8992b$nUN_d90_wequal_any <- with (bmap.y8992, by (nUN_d90_wequal_any, country, max.na))[]
bmap.y8992b$n50_d90_wSize_any <- with (bmap.y8992, by (n50_d90_wSize_any, country, max.na))[]
bmap.y8992b$n50_d90_wSize_any2 <- with (bmap.y8992, by (n50_d90_wSize_any2, country, max.na))[]
bmap.y8992b$autocracy2 <- with (bmap.y8992, by (autocracy, country, mean, na.rm=T))[] ## average over 4-year period
bmap.y8992b$autocracy <- with (bmap.y8992b, ifelse (autocracy2 >= 0.5, 1, 0)) ## depending on average values

# downloading the map for every day of interest
maps <- list ()
map89 <- cshp (date=as.Date ("1989-12-31"))
map92 <- cshp (date=as.Date ("1992-12-31"))
map11 <- cshp (date=as.Date ("2011-12-31"))

# creating the dataset with centroids
cents <- list ()
cents89 <- SpatialPointsDataFrame (coords=coordinates (map89), data=as.data.frame (map89))
cents92 <- SpatialPointsDataFrame (coords=coordinates (map92), data=as.data.frame (map92))
cents11 <- SpatialPointsDataFrame (coords=coordinates (map11), data=as.data.frame (map11))

# merging
map89 <- merge (map89, bmap.y[bmap.y$y89==1,], by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)
cents89 <- merge (cents89, bmap.y[bmap.y$y89==1,], by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)
map8992 <- merge (map92, bmap.y8992b, by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)
cents8992 <- merge (cents92, bmap.y8992b, by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)
map11 <- merge (map11, bmap.y[bmap.y$y11==1,], by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)
cents11 <- merge (cents11, bmap.y[bmap.y$y11==1,], by.x="CNTRY_NAME", by.y="country", all.x=F, all.y=T)

# setting graphical parameters
densDemo <- 70
densAutoc <- 30
cex.text <- 2.25
protCol <- "#d01c8b"
protColsingle <- "#d01c8b"
protNeigh <- "#f1b6da"
protNeighsingle <- "#f1b6da"
colSingle <- "#2ca25f"
protShape <- 21
lwdt <- 0.75

plotWidth <- 1400
aspect.ratio <- 2
par (mar = c (0.25, 0.25, 1, 0.25))

# 1989
png ("Figures/Snapshot1989.png", w=plotWidth, h=plotWidth/aspect.ratio)
plot (map89, col=colSingle, density=ifelse (is.na (map89$autocracy), 0, ifelse (map89$autocracy==1, densAutoc, densDemo)), lwd=lwdt)
points (cents89, pch=ifelse (cents89$n50_d90_wSize_any==0, NA, protShape), cex=cents89$n50_d90_wSize_any2, bg=NA, col=protNeighsingle, lwd=lwdt*4) ## protests in neighboring countries
points (cents89, pch=ifelse (cents89$inplace==0, NA, protShape), cex=cents89$size2, bg=protColsingle)
points (cents89, pch=ifelse (cents89$protend90==0, NA, protShape), cex=cents89$size2, bg=NA, col=protNeighsingle, lwd=lwdt*4) ## protests in neighboring countries, for countries that experienced protests
legend (
  "bottomleft"
  , bty="n"
  , density=c (densDemo, densAutoc)
  , fill=colSingle
  , legend=c ("Democracy", "Autocracy")
  , cex=cex.text*2/3
)
legend (
  x=-192.5, y=-15
  , bty="n"
  , legend=c ("Size: < 1,000", "Size: 1,000-10,000", "Size: 10,000-100,000", "Size: 100,000-1 million", "Size: 1 million or more")
  , pch=19
  , col=protColsingle
  , pt.cex=sqrt (1:5)
  , cex=cex.text*2/3
)
dev.off ()

# 1989-1992
png ("Figures/Snapshot1989-1992.png", w=plotWidth, h=plotWidth/aspect.ratio)
plot (map8992, col=colSingle, density=ifelse (is.na (map8992$autocracy), 0, ifelse (map8992$autocracy==1, densAutoc, densDemo)), lwd=lwdt)
points (cents8992, pch=ifelse (cents8992$n50_d90_wSize_any==0, NA, protShape), cex=cents8992$n50_d90_wSize_any2, bg=NA, col=protNeighsingle, lwd=lwdt*4)
points (cents8992, pch=ifelse (cents8992$inplace==0, NA, protShape), cex=cents8992$size2, bg=protColsingle)
points (cents8992, pch=ifelse (cents8992$protend90==0, NA, protShape), cex=cents8992$size2, bg=NA, col=protNeighsingle, lwd=lwdt*4)
legend (
  "bottomleft"
  , bty="n"
  , density=c (densDemo, densAutoc)
  , fill=colSingle
  , legend=c ("Democracy", "Autocracy")
  , cex=cex.text*2/3
)
legend (
  x=-192.5, y=-15
  , bty="n"
  , legend=c ("Size: < 1,000", "Size: 1,000-10,000", "Size: 10,000-100,000", "Size: 100,000-1 million", "Size: 1 million or more")
  , pch=19
  , col=protColsingle
  , pt.cex=sqrt (1:5)
  , cex=cex.text*2/3
)
dev.off ()

# 2011
png ("Figures/Snapshot2011.png", w=plotWidth, h=plotWidth/aspect.ratio)
plot (map11, col=colSingle, density=ifelse (is.na (map11$autocracy), 0, ifelse (map11$autocracy==1, densAutoc, densDemo)), lwd=lwdt)
points (cents11, pch=ifelse (cents11$n50_d90_wSize_any==0, NA, protShape), cex=cents11$n50_d90_wSize_any2, bg=NA, col=protNeighsingle, lwd=lwdt*4)
points (cents11, pch=ifelse (cents11$inplace==0, NA, protShape), cex=cents11$size2, bg=protColsingle)
points (cents11, pch=ifelse (cents11$protend90==0, NA, protShape), cex=cents11$size2, bg=NA, col=protNeighsingle, lwd=lwdt*4)
legend (
  "bottomleft"
  , bty="n"
  , density=c (densDemo, densAutoc)
  , fill=colSingle
  , legend=c ("Democracy", "Autocracy")
  , cex=cex.text*2/3
)
legend (
  x=-192.5, y=-15
  , bty="n"
  , legend=c ("Size: < 1,000", "Size: 1,000-10,000", "Size: 10,000-100,000", "Size: 100,000-1 million", "Size: 1 million or more")
  , pch=19
  , col=protColsingle
  , pt.cex=sqrt (1:5)
  , cex=cex.text*2/3
)
dev.off ()


### (1.4) Table 3: Number and Size of Democracy Protests, 1989-2011

# first two panels
coefMain01a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mCont01[c (2:4, 7:9)], FUN=function (x) x[1,1]))
  , unlist (lapply (mCont01[c (12:14, 17:19)], FUN=function (x) x[1,1]))
  
  # 800-km neighbors
  , unlist (lapply (mN80001[c (2:4, 7:9)], FUN=function (x) x[1,1]))
  , unlist (lapply (mN80001[c (12:14, 17:19)], FUN=function (x) x[1,1]))
  
  # UN region neighbors
  , unlist (lapply (mUNReg01[c (2:4, 7:9)], FUN=function (x) x[1,1]))
  , unlist (lapply (mUNReg01[c (12:14, 17:19)], FUN=function (x) x[1,1]))
)
pvalMain01a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mCont01[c (2:4, 7:9)], FUN=function (x) x[1,4]))
  , unlist (lapply (mCont01[c (12:14, 17:19)], FUN=function (x) x[1,4]))
  
  # 800-km neighbors
  , unlist (lapply (mN80001[c (2:4, 7:9)], FUN=function (x) x[1,4]))
  , unlist (lapply (mN80001[c (12:14, 17:19)], FUN=function (x) x[1,4]))
  
  # UN region neighbors
  , unlist (lapply (mUNReg01[c (2:4, 7:9)], FUN=function (x) x[1,4]))
  , unlist (lapply (mUNReg01[c (12:14, 17:19)], FUN=function (x) x[1,4]))
)

# third panel: weighting neighbor protests by size
coefMain02a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mCont03[1:3], FUN=function (x) x[1,1]))
  , unlist (lapply (mCont03[19:21], FUN=function (x) x[1,1]))
  
  # 800-km neighbors
  , unlist (lapply (mN80003[1:3], FUN=function (x) x[1,1]))
  , unlist (lapply (mN80003[19:21], FUN=function (x) x[1,1]))
  
  # UN region neighbors
  , unlist (lapply (mUNReg03[1:3], FUN=function (x) x[1,1]))
  , unlist (lapply (mUNReg03[19:21], FUN=function (x) x[1,1]))
)
pvalMain02a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mCont03[1:3], FUN=function (x) x[1,4]))
  , unlist (lapply (mCont03[19:21], FUN=function (x) x[1,4]))
  
  # 800-km neighbors
  , unlist (lapply (mN80003[1:3], FUN=function (x) x[1,4]))
  , unlist (lapply (mN80003[19:21], FUN=function (x) x[1,4]))
  
  # UN region neighbors
  , unlist (lapply (mUNReg03[1:3], FUN=function (x) x[1,4]))
  , unlist (lapply (mUNReg03[19:21], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefMain01 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (cbind (coefMain01a, coefMain02a)), 3)), ncol=9))
pvalMain01 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (cbind (pvalMain01a, pvalMain02a), 3)), ")", sep="")
  , ifelse (as.numeric (cbind (pvalMain01a, pvalMain02a)<=0.05)==0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabMain01 <- rbind (coefMain01, pvalMain01)[c (1, 1+nrow (coefMain01)) + sort (rep ((0:(nrow (coefMain01)-1)), 2)),]
(tabMain01 <- cbind (tabMain01[,1:3]
                     , rep ("", nrow (tabMain01))
                     , tabMain01[,4:6]
                     , rep ("", nrow (tabMain01))
                     , tabMain01[,7:9]))

# building the table
rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01main[1]), " countries; ", sprintf ("%.0f", nobsCont01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001main[1]), " countries; ", sprintf ("%.0f", nobsN80001main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01main[1]), " countries; ", sprintf ("%.0f", nobsUNReg01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabMain01)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Number and Size of Democracy Protests, 1989-2011"
               , label="T:resultsMain")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (1.5) Table 4: Diffusion of Democracy Protests: Alternative Samples

# getting the coefficients
coefASamp01 <- cbind (
  
  # contiguous neighbors
  rbind (
    unlist (lapply (mCont.border[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.int[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.pc[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.y89[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.aspring[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.aut[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.autVD[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.dem[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.demVD[1:3], FUN=function (x) x[1,1]))
    
    , unlist (lapply (mCont.border[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.int[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.pc[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.y89[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.aspring[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.aut[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.autVD[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.dem[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mCont.demVD[4:6], FUN=function (x) x[1,1]))
  )
  
  # 800-km neighbors
  , rbind (
    unlist (lapply (mN800.border[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.int[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.pc[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.y89[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.aspring[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.aut[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.autVD[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.dem[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.demVD[1:3], FUN=function (x) x[1,1]))
    
    , unlist (lapply (mN800.border[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.int[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.pc[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.y89[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.aspring[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.aut[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.autVD[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.dem[4:6], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800.demVD[4:6], FUN=function (x) x[1,1]))
  )
)
pvalASamp01 <- cbind (
  
  # contiguous neighbors
  rbind (
    unlist (lapply (mCont.border[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.int[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.pc[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.y89[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.aspring[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.aut[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.autVD[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.dem[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.demVD[1:3], FUN=function (x) x[1,4]))
    
    , unlist (lapply (mCont.border[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.int[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.pc[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.y89[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.aspring[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.aut[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.autVD[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.dem[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mCont.demVD[4:6], FUN=function (x) x[1,4]))
  )
  
  # 800-km neighbors
  , rbind (
    unlist (lapply (mN800.border[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.int[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.pc[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.y89[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.aspring[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.aut[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.autVD[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.dem[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.demVD[1:3], FUN=function (x) x[1,4]))
    
    , unlist (lapply (mN800.border[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.int[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.pc[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.y89[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.aspring[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.aut[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.autVD[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.dem[4:6], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800.demVD[4:6], FUN=function (x) x[1,4]))
  )
)

# retrieving the estimates
coefASamp01 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefASamp01), 3)), ncol=6))
pvalASamp01 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalASamp01, 3)), ")", sep="")
  , ifelse (as.numeric (pvalASamp01<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefASamp01), ncol=6)
tabASamp01 <- rbind (coefASamp01, pvalASamp01)[c (1, 1+nrow (coefASamp01)) + sort (rep ((0:(nrow (coefASamp01)-1)), 2)),]
tabASamp01 <- cbind (tabASamp01[,1:3]
                     , rep ("", nrow (tabASamp01))
                     , tabASamp01[,4:6])

# building the table
rows <- rep (c ("\\emph{Excluding borderline protests}", ""
                , "\\emph{Internet era only}", ""
                , "\\emph{Fall of Communism (1989-1992)}", ""
                , "\\emph{Fall of Communism (1989 only)}", ""
                , "\\emph{Arab Spring (2010-2011)}", ""
                , "\\emph{Autocracies only} (Polity IV)", ""
                , "\\emph{Autocracies only} (V-Dem)", ""
                , "\\emph{Democracies only} (Polity IV)", ""
                , "\\emph{Democracies only} (V-Dem)", ""), 2)

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n")
Header2 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header3 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline $+$ Election Control}}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 18
addtorow$command <- c (Header1, Header2, Header3)
print (xtable (cbind (rows, tabASamp01)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Diffusion of Democracy Protests: Alternative Samples"
               , label="T:resultsAltSamples")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (1.6) Table 5: Government Responses to Neighbor Protests

# getting the coefficients
coefPOutcome01 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mCont03[4:6], FUN=function (x) x[1,1])) ## success
  , unlist (lapply (mCont03[4:6], FUN=function (x) x[2,1]))
  , unlist (lapply (mCont03[4:6], FUN=function (x) x[3,1]))
  , unlist (lapply (mCont03[10:12], FUN=function (x) x[1,1])) ## repression
  , unlist (lapply (mCont03[10:12], FUN=function (x) x[2,1]))
  , unlist (lapply (mCont03[10:12], FUN=function (x) x[3,1]))
  , unlist (lapply (mCont03[13:15], FUN=function (x) x[1,1])) ## election-related
  
  # contiguous neighbors: baseline + elections
  , unlist (lapply (mCont03[22:24], FUN=function (x) x[1,1])) ## success
  , unlist (lapply (mCont03[22:24], FUN=function (x) x[2,1]))
  , unlist (lapply (mCont03[22:24], FUN=function (x) x[3,1]))
  , unlist (lapply (mCont03[28:30], FUN=function (x) x[1,1])) ## repression
  , unlist (lapply (mCont03[28:30], FUN=function (x) x[2,1]))
  , unlist (lapply (mCont03[28:30], FUN=function (x) x[3,1]))
  , unlist (lapply (mCont03[31:33], FUN=function (x) x[1,1])) ## election-related
)

# 800-km neighbors: baseline
, rbind (
  unlist (lapply (mN80003[4:6], FUN=function (x) x[1,1])) ## success
  , unlist (lapply (mN80003[4:6], FUN=function (x) x[2,1]))
  , unlist (lapply (mN80003[4:6], FUN=function (x) x[3,1]))
  , unlist (lapply (mN80003[10:12], FUN=function (x) x[1,1])) ## repression
  , unlist (lapply (mN80003[10:12], FUN=function (x) x[2,1]))
  , unlist (lapply (mN80003[10:12], FUN=function (x) x[3,1]))
  , unlist (lapply (mN80003[13:15], FUN=function (x) x[1,1])) ## election-related
  
  # contiguous neighbors: baseline + elections
  , unlist (lapply (mN80003[22:24], FUN=function (x) x[1,1])) ## success
  , unlist (lapply (mN80003[22:24], FUN=function (x) x[2,1]))
  , unlist (lapply (mN80003[22:24], FUN=function (x) x[3,1]))
  , unlist (lapply (mN80003[28:30], FUN=function (x) x[1,1])) ## repression
  , unlist (lapply (mN80003[28:30], FUN=function (x) x[2,1]))
  , unlist (lapply (mN80003[28:30], FUN=function (x) x[3,1]))
  , unlist (lapply (mN80003[31:33], FUN=function (x) x[1,1])) ## election-related
)
)
pvalPOutcome01 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mCont03[4:6], FUN=function (x) x[1,4])) ## success
  , unlist (lapply (mCont03[4:6], FUN=function (x) x[2,4]))
  , unlist (lapply (mCont03[4:6], FUN=function (x) x[3,4]))
  , unlist (lapply (mCont03[10:12], FUN=function (x) x[1,4])) ## repression
  , unlist (lapply (mCont03[10:12], FUN=function (x) x[2,4]))
  , unlist (lapply (mCont03[10:12], FUN=function (x) x[3,4]))
  , unlist (lapply (mCont03[13:15], FUN=function (x) x[1,4])) ## election-related
  
  # contiguous neighbors: baseline + elections
  , unlist (lapply (mCont03[22:24], FUN=function (x) x[1,4])) ## success
  , unlist (lapply (mCont03[22:24], FUN=function (x) x[2,4]))
  , unlist (lapply (mCont03[22:24], FUN=function (x) x[3,4]))
  , unlist (lapply (mCont03[28:30], FUN=function (x) x[1,4])) ## repression
  , unlist (lapply (mCont03[28:30], FUN=function (x) x[2,4]))
  , unlist (lapply (mCont03[28:30], FUN=function (x) x[3,4]))
  , unlist (lapply (mCont03[31:33], FUN=function (x) x[1,4])) ## election-related
)

# 800-km neighbors: baseline
, rbind (
  unlist (lapply (mN80003[4:6], FUN=function (x) x[1,4])) ## success
  , unlist (lapply (mN80003[4:6], FUN=function (x) x[2,4]))
  , unlist (lapply (mN80003[4:6], FUN=function (x) x[3,4]))
  , unlist (lapply (mN80003[10:12], FUN=function (x) x[1,4])) ## repression
  , unlist (lapply (mN80003[10:12], FUN=function (x) x[2,4]))
  , unlist (lapply (mN80003[10:12], FUN=function (x) x[3,4]))
  , unlist (lapply (mN80003[13:15], FUN=function (x) x[1,4])) ## election-related
  
  # contiguous neighbors: baseline + elections
  , unlist (lapply (mN80003[22:24], FUN=function (x) x[1,4])) ## success
  , unlist (lapply (mN80003[22:24], FUN=function (x) x[2,4]))
  , unlist (lapply (mN80003[22:24], FUN=function (x) x[3,4]))
  , unlist (lapply (mN80003[28:30], FUN=function (x) x[1,4])) ## repression
  , unlist (lapply (mN80003[28:30], FUN=function (x) x[2,4]))
  , unlist (lapply (mN80003[28:30], FUN=function (x) x[3,4]))
  , unlist (lapply (mN80003[31:33], FUN=function (x) x[1,4])) ## election-related
)
)

# retrieving the estimates
coefPOutcome01 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefPOutcome01), 3)), ncol=6))
pvalPOutcome01 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalPOutcome01, 3)), ")", sep="")
  , ifelse (as.numeric (pvalPOutcome01<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefPOutcome01), ncol=6)
tabPOutcome01 <- rbind (coefPOutcome01, pvalPOutcome01)[c (1, 1+nrow (coefPOutcome01)) + sort (rep ((0:(nrow (coefPOutcome01)-1)), 2)),]
tabPOutcome01 <- cbind (tabPOutcome01[,1:3]
                        , rep ("", nrow (tabPOutcome01))
                        , tabPOutcome01[,4:6])

# building the table
rows <- rep (c ("\\emph{All Successful}", ""
                , "\\emph{Some Successful}", ""
                , "\\emph{None Successful}", ""
                , "\\emph{All Repressed}", ""
                , "\\emph{Some Repressed}", ""
                , "\\emph{None Repressed}", ""
                , "\\emph{Neighbor Democracy Protest}", ""
), 2)

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsCont03[1]), " countries; ", sprintf ("%.0f", nobsCont03[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont03[3]/1000000, 2)), "m obs.)} & & \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsN80003[1]), " countries; ", sprintf ("%.0f", nobsN80003[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80003[3]/1000000, 2)), "m obs.)} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n", sep="")
Header3 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{8}{l}{\\underline{Neighbor protest success}} \\\\ [1.5ex] \n")
Header5 <- paste ("\\multicolumn{8}{l}{\\underline{Neighbor protest repression}} \\\\ [1.5ex] \n")
Header6 <- paste ("\\multicolumn{8}{l}{\\underline{Election-related only}} \\\\ [1.5ex] \n")
Header7 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline $+$ Election Control}}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 6
addtorow$pos[[6]] <- 12
addtorow$pos[[7]] <- 14
addtorow$pos[[8]] <- 14
addtorow$pos[[9]] <- 20
addtorow$pos[[10]] <- 26
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5, Header6, Header7, Header4, Header5, Header6)
print (xtable (cbind (rows, tabPOutcome01)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Government Responses to Neighbor Protests, 1989-2011"
               , label="T:resultsProtResponses")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (1.7) Table 6: Neighbor Countries' Characteristics

# getting the coefficients
coefNChar01 <- cbind (rbind (
  
  # contiguous neighbors
  unlist (lapply (mCont02[1:3], FUN=function (x) x[1,1])) ## GDP (total)
  , unlist (lapply (mCont02[4:6], FUN=function (x) x[1,1])) ## GDP (relative)
  , unlist (lapply (mCont02[10:12], FUN=function (x) x[1,1])) ## population (total)
  , unlist (lapply (mCont02[13:15], FUN=function (x) x[1,1])) ## population (relative)
  , unlist (lapply (mCont02[19:21], FUN=function (x) x[1,1])) ## military spending (total)
  , unlist (lapply (mCont02[22:24], FUN=function (x) x[1,1])) ## military spending (relative)
  , unlist (lapply (mCont02[25:27], FUN=function (x) x[1,1])) ## military personnel (total)
  , unlist (lapply (mCont02[28:30], FUN=function (x) x[1,1])) ## military personnel (relative)
  , unlist (lapply (mCont02[7:9], FUN=function (x) x[1,1])) ## |GDP/head (relative)|
  
  , unlist (lapply (mCont02[16:18], FUN=function (x) x[1,1])) ## |population (relative)|
  , unlist (lapply (mCont02[31:33], FUN=function (x) x[1,1])) ## |repressiveness (relative)|
  , unlist (lapply (mCont02[34:36], FUN=function (x) x[1,1])) ## |polity (relative)|
  , unlist (lapply (mCont02[37:39], FUN=function (x) x[1,1])) ## |polyarchy (relative)|
)

# 800-km neighbors
, rbind (
  unlist (lapply (mN80002[1:3], FUN=function (x) x[1,1])) ## GDP (total)
  , unlist (lapply (mN80002[4:6], FUN=function (x) x[1,1])) ## GDP (relative)
  , unlist (lapply (mN80002[10:12], FUN=function (x) x[1,1])) ## population (total)
  , unlist (lapply (mN80002[13:15], FUN=function (x) x[1,1])) ## population (relative)
  , unlist (lapply (mN80002[19:21], FUN=function (x) x[1,1])) ## military spending (total)
  , unlist (lapply (mN80002[22:24], FUN=function (x) x[1,1])) ## military spending (relative)
  , unlist (lapply (mN80002[25:27], FUN=function (x) x[1,1])) ## military personnel (total)
  , unlist (lapply (mN80002[28:30], FUN=function (x) x[1,1])) ## military personnel (relative)
  , unlist (lapply (mN80002[7:9], FUN=function (x) x[1,1])) ## |GDP/head (relative)|
  
  , unlist (lapply (mN80002[16:18], FUN=function (x) x[1,1])) ## |population (relative)|
  , unlist (lapply (mN80002[31:33], FUN=function (x) x[1,1])) ## |repressiveness (relative)|
  , unlist (lapply (mN80002[34:36], FUN=function (x) x[1,1])) ## |polity (relative)|
  , unlist (lapply (mN80002[37:39], FUN=function (x) x[1,1])) ## |polyarchy (relative)|
)
)
pvalNChar01 <- cbind (rbind (
  
  # contiguous neighbors
  unlist (lapply (mCont02[1:3], FUN=function (x) x[1,4])) ## GDP (total)
  , unlist (lapply (mCont02[4:6], FUN=function (x) x[1,4])) ## GDP (relative)
  , unlist (lapply (mCont02[10:12], FUN=function (x) x[1,4])) ## population (total)
  , unlist (lapply (mCont02[13:15], FUN=function (x) x[1,4])) ## population (relative)
  , unlist (lapply (mCont02[19:21], FUN=function (x) x[1,4])) ## military spending (total)
  , unlist (lapply (mCont02[22:24], FUN=function (x) x[1,4])) ## military spending (relative)
  , unlist (lapply (mCont02[25:27], FUN=function (x) x[1,4])) ## military personnel (total)
  , unlist (lapply (mCont02[28:30], FUN=function (x) x[1,4])) ## military personnel (relative)
  , unlist (lapply (mCont02[7:9], FUN=function (x) x[1,4])) ## |GDP/head (relative)|
  
  , unlist (lapply (mCont02[16:18], FUN=function (x) x[1,4])) ## |population (relative)|
  , unlist (lapply (mCont02[31:33], FUN=function (x) x[1,4])) ## |repressiveness (relative)|
  , unlist (lapply (mCont02[34:36], FUN=function (x) x[1,4])) ## |polity (relative)|
  , unlist (lapply (mCont02[37:39], FUN=function (x) x[1,4])) ## |polyarchy (relative)|
)

# 800-km neighbors
, rbind (
  unlist (lapply (mN80002[1:3], FUN=function (x) x[1,4])) ## GDP (total)
  , unlist (lapply (mN80002[4:6], FUN=function (x) x[1,4])) ## GDP (relative)
  , unlist (lapply (mN80002[10:12], FUN=function (x) x[1,4])) ## population (total)
  , unlist (lapply (mN80002[13:15], FUN=function (x) x[1,4])) ## population (relative)
  , unlist (lapply (mN80002[19:21], FUN=function (x) x[1,4])) ## military spending (total)
  , unlist (lapply (mN80002[22:24], FUN=function (x) x[1,4])) ## military spending (relative)
  , unlist (lapply (mN80002[25:27], FUN=function (x) x[1,4])) ## military personnel (total)
  , unlist (lapply (mN80002[28:30], FUN=function (x) x[1,4])) ## military personnel (relative)
  , unlist (lapply (mN80002[7:9], FUN=function (x) x[1,4])) ## |GDP/head (relative)|
  
  , unlist (lapply (mN80002[16:18], FUN=function (x) x[1,4])) ## |population (relative)|
  , unlist (lapply (mN80002[31:33], FUN=function (x) x[1,4])) ## |repressiveness (relative)|
  , unlist (lapply (mN80002[34:36], FUN=function (x) x[1,4])) ## |polity (relative)|
  , unlist (lapply (mN80002[37:39], FUN=function (x) x[1,4])) ## |polyarchy (relative)|
)
)

# retrieving the estimates
coefNChar01 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefNChar01), 3)), ncol=6))
pvalNChar01 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalNChar01, 3)), ")", sep="")
  , ifelse (as.numeric (pvalNChar01<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefNChar01), ncol=6)
tabNChar01 <- rbind (coefNChar01, pvalNChar01)[c (1, 1+nrow (coefNChar01)) + sort (rep ((0:(nrow (coefNChar01)-1)), 2)),]
tabNChar01 <- cbind (tabNChar01[,1:3]
                     , rep ("", nrow (tabNChar01))
                     , tabNChar01[,4:6])

# building the table
rows <- c ("\\emph{GDP (absolute)} (log)", ""
           , "\\emph{GDP (relative)} (log)", ""
           , "\\emph{Population (absolute)} (log)", ""
           , "\\emph{Population (relative)} (log)", ""
           , "\\emph{Military spending (absolute)} (log)", ""
           , "\\emph{Military spending (relative)} (log)", ""
           , "\\emph{Military personnel (absolute)} (log)", ""
           , "\\emph{Military personnel (relative)} (log)", ""
           , "\\emph{GDP per capita (similarity)}", ""
           , "\\emph{Population (similarity)}", ""
           , "\\emph{Repressiveness (similarity)}", ""
           , "\\emph{Polity IV (similarity)}", ""
           , "\\emph{Polyarchy index (V-Dem) (similarity)}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \n")
Header2a <- paste ("& \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsCont02.low[1]), "-", sprintf ("%.0f", nobsCont02.high[1]), " countries; ", sprintf ("%.0f", nobsCont02.low[2]), "-", sprintf ("%.0f", nobsCont02.high[2]), "} & & \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsN80002.low[1]), "-", sprintf ("%.0f", nobsN80002.high[1]), " countries; ", sprintf ("%.0f", nobsN80002.low[2]), "-", sprintf ("%.0f", nobsN80002.high[2]), "} \\\\ \n", sep="")
Header2b <- paste ("& \\multicolumn{3}{c}{\\scriptsize country-years; ", sprintf ("%.2f", round (nobsCont02.low[3]/1000000, 2)), "-", sprintf ("%.2f", round (nobsCont02.high[3]/1000000, 2)), "m obs.)} & & \\multicolumn{3}{c}{\\scriptsize country-years; ", sprintf ("%.2f", round (nobsN80002.low[3]/1000000, 2)), "-", sprintf ("%.2f", round (nobsN80002.high[3]/1000000, 2)), "m obs.)} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n", sep="")
Header3 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Influentialness of neighbors}}} & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Similarity with neighbors}}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 16
addtorow$command <- c (Header1, Header2a, Header2b, Header3, Header4)
print (xtable (cbind (rows, tabNChar01)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Neighbor Countries' Characteristics, 1989-2011"
               , label="T:resultsNeighChar")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)





########## (2) ONLINE APPENDIX

### (2.1) Descriptive statistics

### (2.1) Figure 2: Neighbor connectivities, 1989-2011

## Creating the maps with connectivities

# getting country boundaries on the last day of the sample
cmap2011 <- cshp (date=as.Date ("2011-12-31"))
cmap2011b <- merge (cmap2011, unique (distances[distances$date=="2011-07-09",c ("ccode1", "date")]), by.x="COWCODE", by.y="ccode1", all.x=F, all.y=F)  ## 2011-07-09 is the last day in the distances data set

# identifying all DAYS in which there was a boundary change according to cshap():
cshap <- cshp ()

# restricting the dataset to countries that existed at some moment between 1/1/1989 and 31/12/2011
cshap$start <- factor (ifelse (cshap$COWSYEAR == -1, NA, paste (cshap$COWSYEAR, cshap$COWSMONTH, cshap$COWSDAY, sep="-")))
cshap$end <- factor (ifelse (cshap$COWEYEAR == -1, NA, paste (cshap$COWEYEAR, cshap$COWEMONTH, cshap$COWEDAY, sep="-")))
cshap$discard <- ifelse (is.na (cshap$end) | as.Date (cshap$end) < as.Date ("1989-01-01") | as.Date (cshap$start) >= as.Date ("2012-01-01"), 1, 0)
cshap2 <- cshap[cshap$discard==0,]
cshap <- NULL
cshap2$discard <- NULL
cshap2$CNTRY_NAME <- factor (cshap2$CNTRY_NAME)

# looping to create neighbor matrices for every date
dates <- sort (unique (distances$date))

listNeigh50 <- listNeigh800 <- listNeighUN <- list ()
listMap <- list ()

for (i in 1:length (dates)){
  
  # selecting the date of interest as well as the corresponding map
  dTemp <- dates[i]
  mTemp <- cshp (date=as.Date (dTemp))
  
  # restricting the sample to countries for which we have neighbor data
  mTemp2 <- merge (mTemp, unique (distances[distances$date==dTemp,c ("ccode1", "date")]), by.x="COWCODE", by.y="ccode1", all.x=F, all.y=F)
  mTemp2$id <- 1:nrow (mTemp2)
  mTemp2$id2 <- 1:nrow (mTemp2)
  listMap[[i]] <- mTemp2
  
  # calculating neighbour distances
  distTemp2 <- merge (distances[distances$date==dTemp,], as.data.frame (mTemp2[,c ("COWCODE", "id")]), by.x="ccode1", by.y="COWCODE", all.x=T, all.y=T)
  distTemp2 <- merge (distTemp2, as.data.frame (mTemp2[,c ("COWCODE", "id2")]), by.x="ccode2", by.y="COWCODE", all.x=T, all.y=T)
  distTemp2$id <- factor (distTemp2$id) ## need to factor; otherwise, there are issues for countries with no neighbors
  distTemp2$id2 <- factor (distTemp2$id2)
  distTemp50 <- distTemp2[distTemp2$d50b==1,]
  distTemp800 <- distTemp2[distTemp2$d800b==1,]
  distTempUN <- distTemp2[distTemp2$dUN==1,]
  
  # creating the neighbor lists
  listNeigh50[[i]] <- mat2listw (with (distTemp50, table (id, id2)))$neighbours
  listNeigh800[[i]] <- mat2listw (with (distTemp800, table (id, id2)))$neighbours
  listNeighUN[[i]] <- mat2listw (with (distTempUN, table (id, id2)))$neighbours
}

# setting the graphical parameters
plotWidth <- 1050
aspect.ratio <- 2
cCents <- "midnightblue"  ## color for centroids
cNeigh <- "palevioletred3"  ## color for lines connecting neighbors
cexCents <- 0.75  ## size of the centroids
cexNeigh <- 0.0375  ## width of lines connecting neighbors

# (a) contiguity neighbors
png ("Figures/Connectivities01_contiguity.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mar=c (0.15, 0.15, 0.15, 0.15))
plot (cmap2011b, lwd=0.00001)
points (coordinates (cmap2011b), pch=16, col=cCents, cex=cexCents)
for (i in 1:length (dates)){
  plot (listNeigh50[[i]], coordinates (listMap[[i]]), add=T, lwd=cexNeigh, col=cNeigh)
}
plot (cmap2011b, lwd=cexCents, add=T)
dev.off ()

# (b) 800-km neighbors
png ("Figures/Connectivities02_800km.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mar=c (0.15, 0.15, 0.15, 0.15))
plot (cmap2011b, lwd=0.00001)
points (coordinates (cmap2011b), pch=16, col=cCents, cex=cexCents)
for (i in 1:length (dates)){
  plot (listNeigh800[[i]], coordinates (listMap[[i]]), add=T, lwd=cexNeigh/2, col=cNeigh)
}
plot (cmap2011b, lwd=cexCents, add=T)
dev.off ()

# (c) same UN region
png ("Figures/Connectivities03_UNRegion.png", w=plotWidth, h=plotWidth/aspect.ratio)
par (mar=c (0.15, 0.15, 0.15, 0.15))
plot (cmap2011b, lwd=0.00001)
points (coordinates (cmap2011b), pch=16, col=cCents, cex=cexCents)
for (i in 1:length (dates)){
  plot (listNeighUN[[i]], coordinates (listMap[[i]]), add=T, lwd=cexNeigh/2, col=cNeigh)
}
plot (cmap2011b, lwd=cexCents, add=T)
dev.off ()


### (2.2) Table A1: Diffusion of Democracy Protests: 360-day Time Lag, 1989-2011

# getting the coefficients
coefRob01a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mCont01[c (5,10)], FUN=function (x) x[1,1]))
  , unlist (lapply (mCont01[c (15,20)], FUN=function (x) x[1,1]))
  
  # 800-km neighbors
  , unlist (lapply (mN80001[c (5,10)], FUN=function (x) x[1,1]))
  , unlist (lapply (mN80001[c (15,20)], FUN=function (x) x[1,1]))
  
  # UN region neighbors
  , unlist (lapply (mUNReg01[c (5,10)], FUN=function (x) x[1,1]))
  , unlist (lapply (mUNReg01[c (15,20)], FUN=function (x) x[1,1])))
pvalRob01a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mCont01[c (5,10)], FUN=function (x) x[1,4]))
  , unlist (lapply (mCont01[c (15,20)], FUN=function (x) x[1,4]))
  
  # 800-km neighbors
  , unlist (lapply (mN80001[c (5,10)], FUN=function (x) x[1,4]))
  , unlist (lapply (mN80001[c (15,20)], FUN=function (x) x[1,4]))
  
  # UN region neighbors
  , unlist (lapply (mUNReg01[c (5,10)], FUN=function (x) x[1,4]))
  , unlist (lapply (mUNReg01[c (15,20)], FUN=function (x) x[1,4])))

# retrieving the estimates
coefRob01 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob01a), 3)), ncol=2))
pvalRob01 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob01a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob01a<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob01a), ncol=2)
(tabRob01 <- rbind (coefRob01, pvalRob01)[c (1, 1+nrow (coefRob01)) + sort (rep ((0:(nrow (coefRob01)-1)), 2)),])

# building the table
rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule \\multicolumn{3}{c}{\\textbf{A. 50km Neighbors}} \\\\ \\midrule \n", sep="")
Header2a <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{1}{c}{\\textbf{dummy}} & \\multicolumn{1}{c}{\\textbf{sum}} \\\\ \\midrule \n")
Header2b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\\\ \\midrule \n")
Header3 <- paste ("[1.5ex] \\multicolumn{3}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header4 <- paste ("\\toprule \\multicolumn{3}{c}{\\textbf{B. 800km Neighbors}} \\\\ \\midrule \n", sep="")
Header5 <- paste ("\\toprule \\multicolumn{3}{c}{\\textbf{C. UN Region Neighbors}} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 2
addtorow$pos[[4]] <- 4
addtorow$pos[[5]] <- 4
addtorow$pos[[6]] <- 6
addtorow$pos[[7]] <- 8
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2a, Header3
                       , Header4, Header2b, Header3
                       , Header5, Header2b, Header3)
print (xtable (cbind (rows, tabRob01)
               , align=c("l","l","c","c")
               , digits=3
               , caption="\\bf Diffusion of Democracy Protests, 1989-2011: 360-day Time Lag, 1989-2011"
               , label="T:results360")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.3) Table A2: Conditional Effect by Internet Connectivity

# getting the coefficients
coefRob02 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mContRob01[1:3], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob01[1:3], FUN=function (x) x[3,1]))
  
  # contiguous neighbors: baseline + elections
  , unlist (lapply (mContRob01[73:75], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob01[73:75], FUN=function (x) x[4,1])))
  
  # 800-km neighbors: baseline
  , rbind (
    unlist (lapply (mN800Rob01[1:3], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800Rob01[1:3], FUN=function (x) x[3,1]))
    
    # contiguous neighbors: baseline + elections
    , unlist (lapply (mN800Rob01[73:75], FUN=function (x) x[1,1]))
    , unlist (lapply (mN800Rob01[73:75], FUN=function (x) x[4,1]))))
pvalRob02 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mContRob01[1:3], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob01[1:3], FUN=function (x) x[3,4]))
  
  # contiguous neighbors: baseline + elections
  , unlist (lapply (mContRob01[73:75], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob01[73:75], FUN=function (x) x[4,4])))
  
  # 800-km neighbors: baseline
  , rbind (
    unlist (lapply (mN800Rob01[1:3], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800Rob01[1:3], FUN=function (x) x[3,4]))
    
    # contiguous neighbors: baseline + elections
    , unlist (lapply (mN800Rob01[73:75], FUN=function (x) x[1,4]))
    , unlist (lapply (mN800Rob01[73:75], FUN=function (x) x[4,4]))))

# retrieving the estimates
coefRob02 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob02), 3)), ncol=6))
pvalRob02 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob02, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob02<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob02), ncol=6)
tabRob02 <- rbind (coefRob02, pvalRob02)[c (1, 1+nrow (coefRob02)) + sort (rep ((0:(nrow (coefRob02)-1)), 2)),]
tabRob02 <- cbind (tabRob02[,1:3]
                   , rep ("", nrow (tabRob02))
                   , tabRob02[,4:6])

# building the table
rows <- rep (c ("\\emph{Neighbor Democracy Protest}", ""
                , "\\emph{Neighbor Democracy Protest}", "~~~~~$\\times$ \\emph{\\% internet users}"), 2)

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsContRob01.low[1]), " c's; ", sprintf ("%.0f", nobsContRob01.low[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsContRob01.low[3]/1000000, 2)), "m obs.)} & & \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsN800Rob01.low[1]), " c's; ", sprintf ("%.0f", nobsN800Rob01.low[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsN800Rob01.low[3]/1000000, 2)), "m obs.)} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n", sep="")
Header3 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 4
addtorow$command <- c (Header1, Header2, Header3, Header4)
print (xtable (cbind (rows, tabRob02)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Conditional Effect by Internet Connectivity"
               , label="T:resultsInt")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.4) Table A3: Robustness (1): Neither FEs nor Clustered SEs

scaling <- 18*0  ## indicates how much we'll move along the list with results

coefRob0901a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob0901a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob0901 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0901a), 3)), ncol=9))
pvalRob0901 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0901a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0901a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0901 <- rbind (coefRob0901, pvalRob0901)[c (1, 1+nrow (coefRob0901)) + sort (rep ((0:(nrow (coefRob0901)-1)), 2)),]
(tabRob0901 <- cbind (tabRob0901[,1:3]
                      , rep ("", nrow (tabRob0901))
                      , tabRob0901[,4:6]
                      , rep ("", nrow (tabRob0901))
                      , tabRob0901[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01main[1]), " countries; ", sprintf ("%.0f", nobsCont01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001main[1]), " countries; ", sprintf ("%.0f", nobsN80001main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01main[1]), " countries; ", sprintf ("%.0f", nobsUNReg01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0901)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (1): Neither FEs nor Clustered SEs"
               , label="T:robustMain01")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.5) Table A4: Robustness (2): No FEs, SEs Clustered by Country

scaling <- 18*1

coefRob0902a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob0902a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob0902 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0902a), 3)), ncol=9))
pvalRob0902 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0902a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0902a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0902 <- rbind (coefRob0902, pvalRob0902)[c (1, 1+nrow (coefRob0902)) + sort (rep ((0:(nrow (coefRob0902)-1)), 2)),]
(tabRob0902 <- cbind (tabRob0902[,1:3]
                      , rep ("", nrow (tabRob0902))
                      , tabRob0902[,4:6]
                      , rep ("", nrow (tabRob0902))
                      , tabRob0902[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01main[1]), " countries; ", sprintf ("%.0f", nobsCont01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001main[1]), " countries; ", sprintf ("%.0f", nobsN80001main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01main[1]), " countries; ", sprintf ("%.0f", nobsUNReg01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0902)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (2): No FEs, SEs Clustered by Country"
               , label="T:robustMain02")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.6) Table A5: Robustness (3): No FEs, SEs clustered by Country-Year

scaling <- 18*2

coefRob0903a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob0903a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob0903 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0903a), 3)), ncol=9))
pvalRob0903 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0903a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0903a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0903 <- rbind (coefRob0903, pvalRob0903)[c (1, 1+nrow (coefRob0903)) + sort (rep ((0:(nrow (coefRob0903)-1)), 2)),]
(tabRob0903 <- cbind (tabRob0903[,1:3]
                      , rep ("", nrow (tabRob0903))
                      , tabRob0903[,4:6]
                      , rep ("", nrow (tabRob0903))
                      , tabRob0903[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01main[1]), " countries; ", sprintf ("%.0f", nobsCont01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001main[1]), " countries; ", sprintf ("%.0f", nobsN80001main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01main[1]), " countries; ", sprintf ("%.0f", nobsUNReg01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0903)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (3): No FEs, SEs Clustered by Country-Year"
               , label="T:robustMain03")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.7) Table A6: Robustness (4): Neither FEs nor Clustered SEs + Controls

scaling <- 18*3

coefRob0904a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob0904a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob0904 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0904a), 3)), ncol=9))
pvalRob0904 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0904a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0904a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0904 <- rbind (coefRob0904, pvalRob0904)[c (1, 1+nrow (coefRob0904)) + sort (rep ((0:(nrow (coefRob0904)-1)), 2)),]
(tabRob0904 <- cbind (tabRob0904[,1:3]
                      , rep ("", nrow (tabRob0904))
                      , tabRob0904[,4:6]
                      , rep ("", nrow (tabRob0904))
                      , tabRob0904[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01controls[1]), " countries; ", sprintf ("%.0f", nobsCont01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001controls[1]), " countries; ", sprintf ("%.0f", nobsN80001controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01controls[1]), " countries; ", sprintf ("%.0f", nobsUNReg01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0904)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (4): Neither FEs nor Clustered SEs $+$ Controls"
               , label="T:robustMain04")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.8) Table A7: Robustness (5): No FEs but SEs Clustered by Country + Controls

scaling <- 18*4

coefRob0905a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob0905a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob0905 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0905a), 3)), ncol=9))
pvalRob0905 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0905a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0905a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0905 <- rbind (coefRob0905, pvalRob0905)[c (1, 1+nrow (coefRob0905)) + sort (rep ((0:(nrow (coefRob0905)-1)), 2)),]
(tabRob0905 <- cbind (tabRob0905[,1:3]
                      , rep ("", nrow (tabRob0905))
                      , tabRob0905[,4:6]
                      , rep ("", nrow (tabRob0905))
                      , tabRob0905[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01controls[1]), " countries; ", sprintf ("%.0f", nobsCont01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001controls[1]), " countries; ", sprintf ("%.0f", nobsN80001controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01controls[1]), " countries; ", sprintf ("%.0f", nobsUNReg01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0905)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (5): No FEs but SEs Clustered by Country $+$ Controls"
               , label="T:robustMain05")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.9) Table A8: Robustness (6): No FEs but SEs Clustered by Country-Year + Controls

scaling <- 18*5

coefRob0906a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob0906a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob0906 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0906a), 3)), ncol=9))
pvalRob0906 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0906a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0906a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0906 <- rbind (coefRob0906, pvalRob0906)[c (1, 1+nrow (coefRob0906)) + sort (rep ((0:(nrow (coefRob0906)-1)), 2)),]
(tabRob0906 <- cbind (tabRob0906[,1:3]
                      , rep ("", nrow (tabRob0906))
                      , tabRob0906[,4:6]
                      , rep ("", nrow (tabRob0906))
                      , tabRob0906[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01controls[1]), " countries; ", sprintf ("%.0f", nobsCont01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001controls[1]), " countries; ", sprintf ("%.0f", nobsN80001controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01controls[1]), " countries; ", sprintf ("%.0f", nobsUNReg01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0906)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (6): No FEs but SEs Clustered by Country-Year $+$ Controls"
               , label="T:robustMain06")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.10) Table A9: Robustness (7): FEs by Country and SEs Clustered by Country

scaling <- 18*6

coefRob0907a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob0907a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob0907 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0907a), 3)), ncol=9))
pvalRob0907 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0907a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0907a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0907 <- rbind (coefRob0907, pvalRob0907)[c (1, 1+nrow (coefRob0907)) + sort (rep ((0:(nrow (coefRob0907)-1)), 2)),]
(tabRob0907 <- cbind (tabRob0907[,1:3]
                      , rep ("", nrow (tabRob0907))
                      , tabRob0907[,4:6]
                      , rep ("", nrow (tabRob0907))
                      , tabRob0907[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01main[1]), " countries; ", sprintf ("%.0f", nobsCont01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001main[1]), " countries; ", sprintf ("%.0f", nobsN80001main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01main[1]), " countries; ", sprintf ("%.0f", nobsUNReg01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0907)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (7): FEs by Country and SEs Clustered by Country"
               , label="T:robustMain07")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.11) Table A10: Robustness (8): FEs by Country and SEs Clustered by Country + Controls

scaling <- 18*7

coefRob0908a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob0908a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob0908 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0908a), 3)), ncol=9))
pvalRob0908 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0908a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0908a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0908 <- rbind (coefRob0908, pvalRob0908)[c (1, 1+nrow (coefRob0908)) + sort (rep ((0:(nrow (coefRob0908)-1)), 2)),]
(tabRob0908 <- cbind (tabRob0908[,1:3]
                      , rep ("", nrow (tabRob0908))
                      , tabRob0908[,4:6]
                      , rep ("", nrow (tabRob0908))
                      , tabRob0908[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01controls[1]), " countries; ", sprintf ("%.0f", nobsCont01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001controls[1]), " countries; ", sprintf ("%.0f", nobsN80001controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01controls[1]), " countries; ", sprintf ("%.0f", nobsUNReg01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0908)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (8): FEs by Country and SEs Clustered by Country $+$ Controls"
               , label="T:robustMain08")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.12) Table A11: Robustness (9): FEs by Country and Year, SEs Clustered by Country

scaling <- 18*8

coefRob0909a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob0909a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob0909 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0909a), 3)), ncol=9))
pvalRob0909 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0909a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0909a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0909 <- rbind (coefRob0909, pvalRob0909)[c (1, 1+nrow (coefRob0909)) + sort (rep ((0:(nrow (coefRob0909)-1)), 2)),]
(tabRob0909 <- cbind (tabRob0909[,1:3]
                      , rep ("", nrow (tabRob0909))
                      , tabRob0909[,4:6]
                      , rep ("", nrow (tabRob0909))
                      , tabRob0909[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01main[1]), " countries; ", sprintf ("%.0f", nobsCont01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001main[1]), " countries; ", sprintf ("%.0f", nobsN80001main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01main[1]), " countries; ", sprintf ("%.0f", nobsUNReg01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0909)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (9): FEs by Country and Year, SEs Clustered by Country"
               , label="T:robustMain09")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.13) Table A12: Robustness (10): FEs by Country and Year, SEs Clustered by Country + Controls

scaling <- 18*9

coefRob0910a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob0910a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob0910 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0910a), 3)), ncol=9))
pvalRob0910 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0910a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0910a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0910 <- rbind (coefRob0910, pvalRob0910)[c (1, 1+nrow (coefRob0910)) + sort (rep ((0:(nrow (coefRob0910)-1)), 2)),]
(tabRob0910 <- cbind (tabRob0910[,1:3]
                      , rep ("", nrow (tabRob0910))
                      , tabRob0910[,4:6]
                      , rep ("", nrow (tabRob0910))
                      , tabRob0910[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01controls[1]), " countries; ", sprintf ("%.0f", nobsCont01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001controls[1]), " countries; ", sprintf ("%.0f", nobsN80001controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01controls[1]), " countries; ", sprintf ("%.0f", nobsUNReg01controls[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01controls[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0910)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (10): FEs by Country and Year, SEs Clustered by Country $+$ Controls"
               , label="T:robustMain10")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.14) Table A13: Robustness (11): FEs by Country-Decade, SEs Clustered by Country

scaling <- 18*10

coefRob0911a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob0911a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob0911 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0911a), 3)), ncol=9))
pvalRob0911 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0911a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0911a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0911 <- rbind (coefRob0911, pvalRob0911)[c (1, 1+nrow (coefRob0911)) + sort (rep ((0:(nrow (coefRob0911)-1)), 2)),]
(tabRob0911 <- cbind (tabRob0911[,1:3]
                      , rep ("", nrow (tabRob0911))
                      , tabRob0911[,4:6]
                      , rep ("", nrow (tabRob0911))
                      , tabRob0911[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01decade[1]), " countries; ", sprintf ("%.0f", nobsCont01decade[2]), " country-decades; $\\approx$ ", sprintf ("%.2f", round (nobsCont01decade[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001decade[1]), " countries; ", sprintf ("%.0f", nobsN80001decade[2]), " country-decades; $\\approx$ ", sprintf ("%.2f", round (nobsN80001decade[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01decade[1]), " countries; ", sprintf ("%.0f", nobsUNReg01decade[2]), " country-decades; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01decade[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0911)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (11): FEs by Country-Decade, SEs Clustered by Country"
               , label="T:robustMain11")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.15) Table A14: Robustness (12): FEs by Country-Decade, SEs Clustered by Country + Controls

scaling <- 18*11

coefRob0912a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob0912a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob0912 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0912a), 3)), ncol=9))
pvalRob0912 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0912a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0912a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0912 <- rbind (coefRob0912, pvalRob0912)[c (1, 1+nrow (coefRob0912)) + sort (rep ((0:(nrow (coefRob0912)-1)), 2)),]
(tabRob0912 <- cbind (tabRob0912[,1:3]
                      , rep ("", nrow (tabRob0912))
                      , tabRob0912[,4:6]
                      , rep ("", nrow (tabRob0912))
                      , tabRob0912[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01decade2[1]), " countries; ", sprintf ("%.0f", nobsCont01decade2[2]), " country-decades; $\\approx$ ", sprintf ("%.2f", round (nobsCont01decade2[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001decade2[1]), " countries; ", sprintf ("%.0f", nobsN80001decade2[2]), " country-decades; $\\approx$ ", sprintf ("%.2f", round (nobsN80001decade2[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01decade2[1]), " countries; ", sprintf ("%.0f", nobsUNReg01decade2[2]), " country-decades; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01decade2[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0912)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (12): FEs by Country-Decade, SEs Clustered by Country $+$ Controls"
               , label="T:robustMain12")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.16) Table A15: Robustness (13): FEs by Region-Year, SEs Clustered by Country

scaling <- 18*12

coefRob0913a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob0913a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob0913 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0913a), 3)), ncol=9))
pvalRob0913 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0913a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0913a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0913 <- rbind (coefRob0913, pvalRob0913)[c (1, 1+nrow (coefRob0913)) + sort (rep ((0:(nrow (coefRob0913)-1)), 2)),]
(tabRob0913 <- cbind (tabRob0913[,1:3]
                      , rep ("", nrow (tabRob0913))
                      , tabRob0913[,4:6]
                      , rep ("", nrow (tabRob0913))
                      , tabRob0913[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01ryear[1]), " countries; ", sprintf ("%.0f", nobsCont01ryear[2]), " region-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01ryear[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001ryear[1]), " countries; ", sprintf ("%.0f", nobsN80001ryear[2]), " region-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001ryear[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01ryear[1]), " countries; ", sprintf ("%.0f", nobsUNReg01ryear[2]), " region-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01ryear[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0913)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (13): FEs by Region-Year, SEs Clustered by Country"
               , label="T:robustMain13")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.17) Table A16: Robustness (14): FEs by Region-Year, SEs Clustered by Country + Controls

scaling <- 18*13

coefRob0914a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob0914a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob0914 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0914a), 3)), ncol=9))
pvalRob0914 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0914a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0914a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0914 <- rbind (coefRob0914, pvalRob0914)[c (1, 1+nrow (coefRob0914)) + sort (rep ((0:(nrow (coefRob0914)-1)), 2)),]
(tabRob0914 <- cbind (tabRob0914[,1:3]
                      , rep ("", nrow (tabRob0914))
                      , tabRob0914[,4:6]
                      , rep ("", nrow (tabRob0914))
                      , tabRob0914[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01ryear2[1]), " countries; ", sprintf ("%.0f", nobsCont01ryear2[2]), " region-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01ryear2[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001ryear2[1]), " countries; ", sprintf ("%.0f", nobsN80001ryear2[2]), " region-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001ryear2[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01ryear2[1]), " countries; ", sprintf ("%.0f", nobsUNReg01ryear2[2]), " region-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01ryear2[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0914)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (14): FEs by Region-Year, SEs Clustered by Country $+$ Controls"
               , label="T:robustMain14")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.18) Table A17: Robustness (15): Logit with REs by Country

scaling <- 18*15

coefRob0916a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob0916a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob0916 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0916a), 3)), ncol=9))
pvalRob0916 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0916a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0916a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0916 <- rbind (coefRob0916, pvalRob0916)[c (1, 1+nrow (coefRob0916)) + sort (rep ((0:(nrow (coefRob0916)-1)), 2)),]
(tabRob0916 <- cbind (tabRob0916[,1:3]
                      , rep ("", nrow (tabRob0916))
                      , tabRob0916[,4:6]
                      , rep ("", nrow (tabRob0916))
                      , tabRob0916[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01main[1]), " countries; ", sprintf ("%.0f", nobsCont01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001main[1]), " countries; ", sprintf ("%.0f", nobsN80001main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01main[1]), " countries; ", sprintf ("%.0f", nobsUNReg01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0916)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (15): Logit with REs by Country"
               , label="T:robustMain15")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.19) Table A18: Robustness (16): Logit with REs by Country-Year

scaling <- 18*16

coefRob0917a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob0917a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob09d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob09d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob09d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob09d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob0917 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob0917a), 3)), ncol=9))
pvalRob0917 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob0917a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob0917a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob0917 <- rbind (coefRob0917, pvalRob0917)[c (1, 1+nrow (coefRob0917)) + sort (rep ((0:(nrow (coefRob0917)-1)), 2)),]
(tabRob0917 <- cbind (tabRob0917[,1:3]
                      , rep ("", nrow (tabRob0917))
                      , tabRob0917[,4:6]
                      , rep ("", nrow (tabRob0917))
                      , tabRob0917[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsCont01main[1]), " countries; ", sprintf ("%.0f", nobsCont01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsCont01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN80001main[1]), " countries; ", sprintf ("%.0f", nobsN80001main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN80001main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNReg01main[1]), " countries; ", sprintf ("%.0f", nobsUNReg01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNReg01main[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob0917)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Robustness (16): Logit with REs by Country-Year"
               , label="T:robustMain16")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.20) Table A19: Weekly Data: Overview

tweek.all <- rbind (
  cbind (rbind (tweekCont04[2,], tweekCont06[2,], tweekCont12[2,], tweekCont18[2,])
         , rbind (tweekN800.04[2,], tweekN800.06[2,], tweekN800.12[2,], tweekN800.18[2,])
         , rbind (tweekUNReg04[2,], tweekUNReg06[2,], tweekUNReg12[2,], tweekUNReg18[2,]))
  , cbind (rbind (tweekCont04abs[2,], tweekCont06abs[2,], tweekCont12abs[2,], tweekCont18abs[2,])
           , rbind (tweekN800.04abs[2,], tweekN800.06abs[2,], tweekN800.12abs[2,], tweekN800.18abs[2,])
           , rbind (tweekUNReg04abs[2,], tweekUNReg06abs[2,], tweekUNReg12abs[2,], tweekUNReg18abs[2,])))
tweek.all2 <- as.data.frame (rbind (
  matrix (paste (sprintf ("%.3f", round (tweek.all[1:4,], 3)), "\\%", sep=""), ncol=6)
  , matrix (paste ("[", sprintf ("%.0f", round (tweek.all[5:8,], 0)), "]", sep=""), ncol=6)))

# adding the asterisks to indicate statistically significant differences
tweek.all2$V2 <- as.character (tweek.all2$V2)
tweek.all2$V4 <- as.character (tweek.all2$V4)
tweek.all2$V6 <- as.character (tweek.all2$V6)
cbind (c (tvalWeekCont04$p.value, tvalWeekCont06$p.value, tvalWeekCont12$p.value, tvalWeekCont18$p.value)
       , c (tvalWeekN800.04$p.value, tvalWeekN800.06$p.value, tvalWeekN800.12$p.value, tvalWeekN800.18$p.value)
       , c (tvalWeekUNReg04$p.value, tvalWeekUNReg06$p.value, tvalWeekUNReg12$p.value, tvalWeekUNReg18$p.value))<0.05 ## four cases
tweek.all2[2:4,4] <- paste (tweek.all2[2:4,4], "*", sep="")
tweek.all2[3,2] <- paste (tweek.all2[3,2], "*", sep="")

odd <- seq (1, nrow (tweek.all), by=2); even <- seq (2, nrow (tweek.all), by=2)
row.names (tweek.all2) <- as.numeric (c (odd, even))
tweek.all2 <- as.matrix (tweek.all2[order (as.numeric (row.names (tweek.all2))),])
(tweek.all3 <- cbind (
  tweek.all2[,1:2]
  , rep ("", nrow(tweek.all))
  , tweek.all2[,3:4]
  , rep ("", nrow(tweek.all))
  , tweek.all2[,5:6]))

rows <- c ("\\textbf{4 weeks}", "", "\\textbf{6 weeks}", "", "\\textbf{12 weeks}", "", "\\textbf{18 weeks}", "")

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\textbf{$50$km}} & & \\multicolumn{2}{c}{\\textbf{$800$km}} & & \\multicolumn{2}{c}{\\textbf{UN region}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} \\\\ \\cmidrule{2-3} \\cmidrule{5-6} \\cmidrule{8-9} \n")
Header3 <- paste ("\\multicolumn{1}{l}{} & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} \\\\ \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{protest in last...}} & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$command <- c (Header1, Header2, Header3, Header4)
print (xtable (cbind (rows, tweek.all3)
               , align=c("c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Weekly Data: Overview"
               , label="T:overviewWeekly")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.21) Table A20: Weekly Data (1): Neither FEs nor Clustered SEs

scaling <- 18*0  ## indicates how much we'll move along the list with results

coefRob1001a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1001a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1001 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1001a), 3)), ncol=9))
pvalRob1001 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1001a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1001a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1001 <- rbind (coefRob1001, pvalRob1001)[c (1, 1+nrow (coefRob1001)) + sort (rep ((0:(nrow (coefRob1001)-1)), 2)),]
(tabRob1001 <- cbind (tabRob1001[,1:3]
                      , rep ("", nrow (tabRob1001))
                      , tabRob1001[,4:6]
                      , rep ("", nrow (tabRob1001))
                      , tabRob1001[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nweeksCont01main[1]), " countries; ", sprintf ("%.0f", nweeksCont01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksCont01main[3], 0))," obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nweeksN80001main[1]), " countries; ", sprintf ("%.0f", nweeksN80001main[2]), " country-years; ", sprintf ("%.0f", round (nweeksN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nweeksUNReg01main[1]), " countries; ", sprintf ("%.0f", nweeksUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1001)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Weekly Data (1): Neither FEs nor Clustered SEs"
               , label="T:robustWeekMain01")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.22) Table A21: Weekly Data (2): No FEs, SEs Clusteed by Country

scaling <- 18*1

coefRob1002a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1002a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1002 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1002a), 3)), ncol=9))
pvalRob1002 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1002a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1002a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1002 <- rbind (coefRob1002, pvalRob1002)[c (1, 1+nrow (coefRob1002)) + sort (rep ((0:(nrow (coefRob1002)-1)), 2)),]
(tabRob1002 <- cbind (tabRob1002[,1:3]
                      , rep ("", nrow (tabRob1002))
                      , tabRob1002[,4:6]
                      , rep ("", nrow (tabRob1002))
                      , tabRob1002[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nweeksCont01main[1]), " countries; ", sprintf ("%.0f", nweeksCont01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nweeksN80001main[1]), " countries; ", sprintf ("%.0f", nweeksN80001main[2]), " country-years; ", sprintf ("%.0f", round (nweeksN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nweeksUNReg01main[1]), " countries; ", sprintf ("%.0f", nweeksUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1002)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Weekly Data (2): No FEs, SEs Clustered by Country"
               , label="T:robustWeekMain02")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.23) Table A22: Weekly Data (3): No FEs, SEs Clusteed by Country + Controls

scaling <- 18*2

coefRob1003a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1003a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1003 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1003a), 3)), ncol=9))
pvalRob1003 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1003a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1003a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1003 <- rbind (coefRob1003, pvalRob1003)[c (1, 1+nrow (coefRob1003)) + sort (rep ((0:(nrow (coefRob1003)-1)), 2)),]
(tabRob1003 <- cbind (tabRob1003[,1:3]
                      , rep ("", nrow (tabRob1003))
                      , tabRob1003[,4:6]
                      , rep ("", nrow (tabRob1003))
                      , tabRob1003[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nweeksCont01controls[1]), " countries; ", sprintf ("%.0f", nweeksCont01controls[2]), " country-years; ", sprintf ("%.0f", round (nweeksCont01controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nweeksN80001controls[1]), " countries; ", sprintf ("%.0f", nweeksN80001controls[2]), " country-years; ", sprintf ("%.0f", round (nweeksN80001controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nweeksUNReg01controls[1]), " countries; ", sprintf ("%.0f", nweeksUNReg01controls[2]), " country-years; ", sprintf ("%.0f", round (nweeksUNReg01controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1003)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Weekly Data (3): No FEs but SEs Clustered by Country $+$ Controls"
               , label="T:robustWeekMain03")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.24) Table A23: Weekly Data (4): FEs by Country and SEs Clustered by Country

scaling <- 18*3

coefRob1004a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob1004a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob1004 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1004a), 3)), ncol=9))
pvalRob1004 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1004a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1004a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1004 <- rbind (coefRob1004, pvalRob1004)[c (1, 1+nrow (coefRob1004)) + sort (rep ((0:(nrow (coefRob1004)-1)), 2)),]
(tabRob1004 <- cbind (tabRob1004[,1:3]
                      , rep ("", nrow (tabRob1004))
                      , tabRob1004[,4:6]
                      , rep ("", nrow (tabRob1004))
                      , tabRob1004[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nweeksCont01main[1]), " countries; ", sprintf ("%.0f", nweeksCont01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nweeksN80001main[1]), " countries; ", sprintf ("%.0f", nweeksN80001main[2]), " country-years; ", sprintf ("%.0f", round (nweeksN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nweeksUNReg01main[1]), " countries; ", sprintf ("%.0f", nweeksUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1004)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Weekly Data (4): FEs by Country and SEs Clustered by Country"
               , label="T:robustWeekMain04")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.25) Table A24: Weekly Data (5): FEs by Country and SEs Clustered by Country + Controls

scaling <- 18*4

coefRob1005a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob1005a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob1005 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1005a), 3)), ncol=9))
pvalRob1005 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1005a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1005a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1005 <- rbind (coefRob1005, pvalRob1005)[c (1, 1+nrow (coefRob1005)) + sort (rep ((0:(nrow (coefRob1005)-1)), 2)),]
(tabRob1005 <- cbind (tabRob1005[,1:3]
                      , rep ("", nrow (tabRob1005))
                      , tabRob1005[,4:6]
                      , rep ("", nrow (tabRob1005))
                      , tabRob1005[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nweeksCont01controls[1]), " countries; ", sprintf ("%.0f", nweeksCont01controls[2]), " country-years; ", sprintf ("%.0f", round (nweeksCont01controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nweeksN80001controls[1]), " countries; ", sprintf ("%.0f", nweeksN80001controls[2]), " country-years; ", sprintf ("%.0f", round (nweeksN80001controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nweeksUNReg01controls[1]), " countries; ", sprintf ("%.0f", nweeksUNReg01controls[2]), " country-years; ", sprintf ("%.0f", round (nweeksUNReg01controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1005)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Weekly Data (5): FEs by Country and SEs Clustered by Country $+$ Controls"
               , label="T:robustWeekMain05")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.26) Table A25: Weekly Data (6): FEs by Country-Year and SEs Clustered by Country

scaling <- 18*5

coefRob1006a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob1006a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob1006 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1006a), 3)), ncol=9))
pvalRob1006 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1006a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1006a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1006 <- rbind (coefRob1006, pvalRob1006)[c (1, 1+nrow (coefRob1006)) + sort (rep ((0:(nrow (coefRob1006)-1)), 2)),]
(tabRob1006 <- cbind (tabRob1006[,1:3]
                      , rep ("", nrow (tabRob1006))
                      , tabRob1006[,4:6]
                      , rep ("", nrow (tabRob1006))
                      , tabRob1006[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nweeksCont01main[1]), " countries; ", sprintf ("%.0f", nweeksCont01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nweeksN80001main[1]), " countries; ", sprintf ("%.0f", nweeksN80001main[2]), " country-years; ", sprintf ("%.0f", round (nweeksN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nweeksUNReg01main[1]), " countries; ", sprintf ("%.0f", nweeksUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1006)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Weekly Data (6): FEs by Country-Year and SEs Clustered by Country"
               , label="T:robustWeekMain06")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.27) Table A26: Weekly Data (7): Logit with REs by Country

scaling <- 18*6

coefRob1007a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1007a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1007 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1007a), 3)), ncol=9))
pvalRob1007 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1007a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1007a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1007 <- rbind (coefRob1007, pvalRob1007)[c (1, 1+nrow (coefRob1007)) + sort (rep ((0:(nrow (coefRob1007)-1)), 2)),]
(tabRob1007 <- cbind (tabRob1007[,1:3]
                      , rep ("", nrow (tabRob1007))
                      , tabRob1007[,4:6]
                      , rep ("", nrow (tabRob1007))
                      , tabRob1007[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nweeksCont01main[1]), " countries; ", sprintf ("%.0f", nweeksCont01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nweeksN80001main[1]), " countries; ", sprintf ("%.0f", nweeksN80001main[2]), " country-years; ", sprintf ("%.0f", round (nweeksN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nweeksUNReg01main[1]), " countries; ", sprintf ("%.0f", nweeksUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1007)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Weekly Data (7): Logit with REs by Country"
               , label="T:robustWeekMain07")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.28) Table A27: Weekly Data (8): Logit with REs by Country-Year

scaling <- 18*7

coefRob1008a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1008a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob10w06[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob10w06[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob10w06[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w12[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob10w18[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1008 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1008a), 3)), ncol=9))
pvalRob1008 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1008a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1008a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1008 <- rbind (coefRob1008, pvalRob1008)[c (1, 1+nrow (coefRob1008)) + sort (rep ((0:(nrow (coefRob1008)-1)), 2)),]
(tabRob1008 <- cbind (tabRob1008[,1:3]
                      , rep ("", nrow (tabRob1008))
                      , tabRob1008[,4:6]
                      , rep ("", nrow (tabRob1008))
                      , tabRob1008[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} & & \\multicolumn{1}{c}{\\textbf{6 weeks}} & \\multicolumn{1}{c}{\\textbf{12 weeks}} & \\multicolumn{1}{c}{\\textbf{18 weeks}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nweeksCont01main[1]), " countries; ", sprintf ("%.0f", nweeksCont01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nweeksN80001main[1]), " countries; ", sprintf ("%.0f", nweeksN80001main[2]), " country-years; ", sprintf ("%.0f", round (nweeksN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nweeksUNReg01main[1]), " countries; ", sprintf ("%.0f", nweeksUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nweeksUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1008)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Weekly Data (8): Logit with REs by Country-Year"
               , label="T:robustWeekMain08")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.29) Table A28: Monthly Data: Overview

tmonth.all <- rbind (
  cbind (rbind (tmonthCont01[2,], tmonthCont02[2,], tmonthCont03[2,], tmonthCont04[2,])
         , rbind (tmonthN800.01[2,], tmonthN800.02[2,], tmonthN800.03[2,], tmonthN800.04[2,])
         , rbind (tmonthUNReg01[2,], tmonthUNReg02[2,], tmonthUNReg03[2,], tmonthUNReg04[2,]))
  , cbind (rbind (tmonthCont01abs[2,], tmonthCont02abs[2,], tmonthCont03abs[2,], tmonthCont04abs[2,])
           , rbind (tmonthN800.01abs[2,], tmonthN800.02abs[2,], tmonthN800.03abs[2,], tmonthN800.04abs[2,])
           , rbind (tmonthUNReg01abs[2,], tmonthUNReg02abs[2,], tmonthUNReg03abs[2,], tmonthUNReg04abs[2,])))
tmonth.all2 <- as.data.frame (rbind (
  matrix (paste (sprintf ("%.3f", round (tmonth.all[1:4,], 3)), "\\%", sep=""), ncol=6)
  , matrix (paste ("[", sprintf ("%.0f", round (tmonth.all[5:8,], 0)), "]", sep=""), ncol=6)))

# adding the asterisks to indicate statistically significant differences
tmonth.all2$V2 <- as.character (tmonth.all2$V2)
tmonth.all2$V4 <- as.character (tmonth.all2$V4)
tmonth.all2$V6 <- as.character (tmonth.all2$V6)
cbind (c (tvalMonthCont01$p.value, tvalMonthCont02$p.value, tvalMonthCont03$p.value, tvalMonthCont04$p.value)
       , c (tvalMonthN800.01$p.value, tvalMonthN800.02$p.value, tvalMonthN800.03$p.value, tvalMonthN800.04$p.value)
       , c (tvalMonthUNReg01$p.value, tvalMonthUNReg02$p.value, tvalMonthUNReg03$p.value, tvalMonthUNReg04$p.value))<0.05 ## four cases
tmonth.all2[2:4,4] <- paste (tmonth.all2[2:4,4], "*", sep="")
tmonth.all2[3,2] <- paste (tmonth.all2[3,2], "*", sep="")

odd <- seq (1, nrow (tmonth.all), by=2); even <- seq (2, nrow (tmonth.all), by=2)
row.names (tmonth.all2) <- as.numeric (c (odd, even))
tmonth.all2 <- as.matrix (tmonth.all2[order (as.numeric (row.names (tmonth.all2))),])
(tmonth.all3 <- cbind (
  tmonth.all2[,1:2]
  , rep ("", nrow(tmonth.all))
  , tmonth.all2[,3:4]
  , rep ("", nrow(tmonth.all))
  , tmonth.all2[,5:6]))

rows <- c ("\\textbf{1 month}", "", "\\textbf{2 months}", "", "\\textbf{3 months}", "", "\\textbf{4 months}", "")

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\textbf{$50$km}} & & \\multicolumn{2}{c}{\\textbf{$800$km}} & & \\multicolumn{2}{c}{\\textbf{UN region}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} \\\\ \\cmidrule{2-3} \\cmidrule{5-6} \\cmidrule{8-9} \n")
Header3 <- paste ("\\multicolumn{1}{l}{} & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} \\\\ \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{protest in last...}} & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$command <- c (Header1, Header2, Header3, Header4)
print (xtable (cbind (rows, tmonth.all3)
               , align=c("c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Monthly Data: Overview"
               , label="T:overviewMonthly")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.30) Table A29: Monthly Data (1): Neither FEs nor Clustered SEs

scaling <- 18*0  ## indicates how much we'll move along the list with results

coefRob1101a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1101a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1101 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1101a), 3)), ncol=9))
pvalRob1101 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1101a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1101a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1101 <- rbind (coefRob1101, pvalRob1101)[c (1, 1+nrow (coefRob1101)) + sort (rep ((0:(nrow (coefRob1101)-1)), 2)),]
(tabRob1101 <- cbind (tabRob1101[,1:3]
                      , rep ("", nrow (tabRob1101))
                      , tabRob1101[,4:6]
                      , rep ("", nrow (tabRob1101))
                      , tabRob1101[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nmonthsCont01main[1]), " countries; ", sprintf ("%.0f", nmonthsCont01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nmonthsN80001main[1]), " countries; ", sprintf ("%.0f", nmonthsN80001main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nmonthsUNReg01main[1]), " countries; ", sprintf ("%.0f", nmonthsUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1101)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Monthly Data (1): Neither FEs nor Clustered SEs"
               , label="T:robustMonthMain01")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.31) Table A30: Monthly Data (2): No FEs, SEs Clusteed by Country

scaling <- 18*1

coefRob1102a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1102a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1102 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1102a), 3)), ncol=9))
pvalRob1102 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1102a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1102a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1102 <- rbind (coefRob1102, pvalRob1102)[c (1, 1+nrow (coefRob1102)) + sort (rep ((0:(nrow (coefRob1102)-1)), 2)),]
(tabRob1102 <- cbind (tabRob1102[,1:3]
                      , rep ("", nrow (tabRob1102))
                      , tabRob1102[,4:6]
                      , rep ("", nrow (tabRob1102))
                      , tabRob1102[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nmonthsCont01main[1]), " countries; ", sprintf ("%.0f", nmonthsCont01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nmonthsN80001main[1]), " countries; ", sprintf ("%.0f", nmonthsN80001main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nmonthsUNReg01main[1]), " countries; ", sprintf ("%.0f", nmonthsUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1102)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Monthly Data (2): No FEs, SEs Clustered by Country"
               , label="T:robustMonthMain02")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.32) Table A31: Monthly Data (3): No FEs, SEs Clusteed by Country + Controls

scaling <- 18*2

coefRob1103a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1103a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1103 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1103a), 3)), ncol=9))
pvalRob1103 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1103a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1103a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1103 <- rbind (coefRob1103, pvalRob1103)[c (1, 1+nrow (coefRob1103)) + sort (rep ((0:(nrow (coefRob1103)-1)), 2)),]
(tabRob1103 <- cbind (tabRob1103[,1:3]
                      , rep ("", nrow (tabRob1103))
                      , tabRob1103[,4:6]
                      , rep ("", nrow (tabRob1103))
                      , tabRob1103[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nmonthsCont01controls[1]), " countries; ", sprintf ("%.0f", nmonthsCont01controls[2]), " country-years; ", sprintf ("%.0f", round (nmonthsCont01controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nmonthsN80001controls[1]), " countries; ", sprintf ("%.0f", nmonthsN80001controls[2]), " country-years; ", sprintf ("%.0f", round (nmonthsN80001controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nmonthsUNReg01controls[1]), " countries; ", sprintf ("%.0f", nmonthsUNReg01controls[2]), " country-years; ", sprintf ("%.0f", round (nmonthsUNReg01controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1103)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Monthly Data (3): No FEs but SEs Clustered by Country $+$ Controls"
               , label="T:robustMonthMain03")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.33) Table A32: Monthly Data (4): FEs by Country and SEs Clustered by Country

scaling <- 18*3

coefRob1104a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob1104a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob1104 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1104a), 3)), ncol=9))
pvalRob1104 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1104a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1104a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1104 <- rbind (coefRob1104, pvalRob1104)[c (1, 1+nrow (coefRob1104)) + sort (rep ((0:(nrow (coefRob1104)-1)), 2)),]
(tabRob1104 <- cbind (tabRob1104[,1:3]
                      , rep ("", nrow (tabRob1104))
                      , tabRob1104[,4:6]
                      , rep ("", nrow (tabRob1104))
                      , tabRob1104[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nmonthsCont01main[1]), " countries; ", sprintf ("%.0f", nmonthsCont01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nmonthsN80001main[1]), " countries; ", sprintf ("%.0f", nmonthsN80001main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nmonthsUNReg01main[1]), " countries; ", sprintf ("%.0f", nmonthsUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1104)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Monthly Data (4): FEs by Country and SEs Clustered by Country"
               , label="T:robustMonthMain04")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.34) Table A33: Monthly Data (5): FEs by Country and SEs Clustered by Country + Controls

scaling <- 18*4

coefRob1105a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob1105a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob1105 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1105a), 3)), ncol=9))
pvalRob1105 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1105a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1105a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1105 <- rbind (coefRob1105, pvalRob1105)[c (1, 1+nrow (coefRob1105)) + sort (rep ((0:(nrow (coefRob1105)-1)), 2)),]
(tabRob1105 <- cbind (tabRob1105[,1:3]
                      , rep ("", nrow (tabRob1105))
                      , tabRob1105[,4:6]
                      , rep ("", nrow (tabRob1105))
                      , tabRob1105[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nmonthsCont01controls[1]), " countries; ", sprintf ("%.0f", nmonthsCont01controls[2]), " country-years; ", sprintf ("%.0f", round (nmonthsCont01controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nmonthsN80001controls[1]), " countries; ", sprintf ("%.0f", nmonthsN80001controls[2]), " country-years; ", sprintf ("%.0f", round (nmonthsN80001controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nmonthsUNReg01controls[1]), " countries; ", sprintf ("%.0f", nmonthsUNReg01controls[2]), " country-years; ", sprintf ("%.0f", round (nmonthsUNReg01controls[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1105)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Monthly Data (5): FEs by Country and SEs Clustered by Country $+$ Controls"
               , label="T:robustMonthMain05")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.35) Table A34: Monthly Data (6): FEs by Country-Year and SEs Clustered by Country

scaling <- 18*5

coefRob1106a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob1106a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob1106 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1106a), 3)), ncol=9))
pvalRob1106 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1106a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1106a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1106 <- rbind (coefRob1106, pvalRob1106)[c (1, 1+nrow (coefRob1106)) + sort (rep ((0:(nrow (coefRob1106)-1)), 2)),]
(tabRob1106 <- cbind (tabRob1106[,1:3]
                      , rep ("", nrow (tabRob1106))
                      , tabRob1106[,4:6]
                      , rep ("", nrow (tabRob1106))
                      , tabRob1106[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nmonthsCont01main[1]), " countries; ", sprintf ("%.0f", nmonthsCont01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nmonthsN80001main[1]), " countries; ", sprintf ("%.0f", nmonthsN80001main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nmonthsUNReg01main[1]), " countries; ", sprintf ("%.0f", nmonthsUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1106)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Monthly Data (6): FEs by Country-Year and SEs Clustered by Country"
               , label="T:robustMonthMain06")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.36) Table A35: Monthly Data (7): Logit with REs by Country

scaling <- 18*6

coefRob1107a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1107a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1107 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1107a), 3)), ncol=9))
pvalRob1107 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1107a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1107a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1107 <- rbind (coefRob1107, pvalRob1107)[c (1, 1+nrow (coefRob1107)) + sort (rep ((0:(nrow (coefRob1107)-1)), 2)),]
(tabRob1107 <- cbind (tabRob1107[,1:3]
                      , rep ("", nrow (tabRob1107))
                      , tabRob1107[,4:6]
                      , rep ("", nrow (tabRob1107))
                      , tabRob1107[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nmonthsCont01main[1]), " countries; ", sprintf ("%.0f", nmonthsCont01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nmonthsN80001main[1]), " countries; ", sprintf ("%.0f", nmonthsN80001main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nmonthsUNReg01main[1]), " countries; ", sprintf ("%.0f", nmonthsUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1107)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Monthly Data (7): Logit with REs by Country"
               , label="T:robustMonthMain07")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.37) Table A36: Monthly Data (8): Logit with REs by Country-Year

scaling <- 18*7

coefRob1108a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1108a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob11m02[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob11m02[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob11m02[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m03[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob11m04[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1108 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1108a), 3)), ncol=9))
pvalRob1108 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1108a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1108a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1108 <- rbind (coefRob1108, pvalRob1108)[c (1, 1+nrow (coefRob1108)) + sort (rep ((0:(nrow (coefRob1108)-1)), 2)),]
(tabRob1108 <- cbind (tabRob1108[,1:3]
                      , rep ("", nrow (tabRob1108))
                      , tabRob1108[,4:6]
                      , rep ("", nrow (tabRob1108))
                      , tabRob1108[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} & & \\multicolumn{1}{c}{\\textbf{2 mths.}} & \\multicolumn{1}{c}{\\textbf{3 mths.}} & \\multicolumn{1}{c}{\\textbf{4 mths.}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nmonthsCont01main[1]), " countries; ", sprintf ("%.0f", nmonthsCont01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nmonthsN80001main[1]), " countries; ", sprintf ("%.0f", nmonthsN80001main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nmonthsUNReg01main[1]), " countries; ", sprintf ("%.0f", nmonthsUNReg01main[2]), " country-years; ", sprintf ("%.0f", round (nmonthsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1108)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Monthly Data (8): Logit with REs by Country-Year"
               , label="T:robustMonthMain08")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.38) Table A37: Yearly Data: Overview

tyear.all <- rbind (
  c (tyearCont01[2,], tyearN800.01[2,], tyearUNReg01[2,])
  , c (tyearCont01abs[2,], tyearN800.01abs[2,], tyearUNReg01abs[2,]))
tyear.all2 <- as.data.frame (rbind (
  matrix (paste (sprintf ("%.3f", round (tyear.all[1,], 3)), "\\%", sep=""), ncol=6)
  , matrix (paste ("[", sprintf ("%.0f", round (tyear.all[2,], 0)), "]", sep=""), ncol=6)))

# adding the asterisks to indicate statistically significant differences
tyear.all2$V2 <- as.character (tyear.all2$V2)
tyear.all2$V4 <- as.character (tyear.all2$V4)
tyear.all2$V6 <- as.character (tyear.all2$V6)
c (tvalYearCont01$p.value, tvalYearN800.01$p.value, tvalYearUNReg01$p.value)<0.05   ## no cases

odd <- seq (1, nrow (tyear.all), by=2); even <- seq (2, nrow (tyear.all), by=2)
row.names (tyear.all2) <- as.numeric (c (odd, even))
tyear.all2 <- as.matrix (tyear.all2[order (as.numeric (row.names (tyear.all2))),])
(tyear.all3 <- cbind (
  tyear.all2[,1:2]
  , rep ("", nrow(tyear.all))
  , tyear.all2[,3:4]
  , rep ("", nrow(tyear.all))
  , tyear.all2[,5:6]))

rows <- c ("\\textbf{1 year}", "")

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\textbf{$50$km}} & & \\multicolumn{2}{c}{\\textbf{$800$km}} & & \\multicolumn{2}{c}{\\textbf{UN region}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} \\\\ \\cmidrule{2-3} \\cmidrule{5-6} \\cmidrule{8-9} \n")
Header3 <- paste ("\\multicolumn{1}{l}{} & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} \\\\ \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{protest in last...}} & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$command <- c (Header1, Header2, Header3, Header4)
print (xtable (cbind (rows, tyear.all3)
               , align=c("c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Yearly Data: Overview"
               , label="T:overviewYearly")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.39) Table A38: Yearly Data (1): Neither FEs nor Clustered SEs

scaling <- 18*0  ## indicates how much we'll move along the list with results

coefRob1201a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1201a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1201 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1201a), 3)), ncol=3))
pvalRob1201 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1201a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1201a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=3)
tabRob1201 <- rbind (coefRob1201, pvalRob1201)[c (1, 1+nrow (coefRob1201)) + sort (rep ((0:(nrow (coefRob1201)-1)), 2)),]
(tabRob1201 <- cbind (tabRob1201[,1]
                      , rep ("", nrow (tabRob1201))
                      , tabRob1201[,2]
                      , rep ("", nrow (tabRob1201))
                      , tabRob1201[,3]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{1}{c}{\\textbf{At Least One}} & & \\multicolumn{1}{c}{\\textbf{Total Number of}} & & \\multicolumn{1}{c}{\\textbf{Size of Largest}} \\\\ \\cmidrule{2-2} \\cmidrule{4-4} \\cmidrule{6-6} \n")
Header2 <- paste (" & \\multicolumn{5}{c}{\\textbf{Neighbor Protest(s) in Previous Year}} \\\\ \\midrule \n")
Header4 <- paste (" & \\multicolumn{5}{c}{\\textbf{A. 50km Neighbors}} \\\\ \n", sep="")
Header4b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsCont01main[1]), " countries; ", sprintf ("%.0f", round (nyearsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{6}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{B. 800km Neighbors}} \\\\ \n", sep="")
Header6b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsN80001main[1]), " countries; ", sprintf ("%.0f", round (nyearsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{C. UN Region Neighbors}} \\\\ \n", sep="")
Header7b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsUNReg01main[1]), " countries; ", sprintf ("%.0f", round (nyearsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 4
addtorow$pos[[8]] <- 6
addtorow$pos[[9]] <- 8
addtorow$pos[[10]] <- 8
addtorow$pos[[11]] <- 10
addtorow$command <- c (Header1, Header3, Header4, Header4b, Header5
                       , Header6, Header6b, Header5, Header7, Header7b, Header5)
print (xtable (cbind (rows, tabRob1201)
               , align=c("l","l","c","c","c","c","c")
               , digits=3
               , caption="\\bf Yearly Data (1): Neither FEs nor Clustered SEs"
               , label="T:robustYearMain01")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.40) Table A39: Yearly Data (2): No FEs, SEs Clusteed by Country

scaling <- 18*1

coefRob1202a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1202a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1202 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1202a), 3)), ncol=3))
pvalRob1202 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1202a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1202a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=3)
tabRob1202 <- rbind (coefRob1202, pvalRob1202)[c (1, 1+nrow (coefRob1202)) + sort (rep ((0:(nrow (coefRob1202)-1)), 2)),]
(tabRob1202 <- cbind (tabRob1202[,1]
                      , rep ("", nrow (tabRob1202))
                      , tabRob1202[,2]
                      , rep ("", nrow (tabRob1202))
                      , tabRob1202[,3]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{1}{c}{\\textbf{At Least One}} & & \\multicolumn{1}{c}{\\textbf{Total Number of}} & & \\multicolumn{1}{c}{\\textbf{Size of Largest}} \\\\ \\cmidrule{2-2} \\cmidrule{4-4} \\cmidrule{6-6} \n")
Header2 <- paste (" & \\multicolumn{5}{c}{\\textbf{Neighbor Protest(s) in Previous Year}} \\\\ \\midrule \n")
Header4 <- paste (" & \\multicolumn{5}{c}{\\textbf{A. 50km Neighbors}} \\\\ \n", sep="")
Header4b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsCont01main[1]), " countries; ", sprintf ("%.0f", round (nyearsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{6}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{B. 800km Neighbors}} \\\\ \n", sep="")
Header6b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsN80001main[1]), " countries; ", sprintf ("%.0f", round (nyearsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{C. UN Region Neighbors}} \\\\ \n", sep="")
Header7b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsUNReg01main[1]), " countries; ", sprintf ("%.0f", round (nyearsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 4
addtorow$pos[[8]] <- 6
addtorow$pos[[9]] <- 8
addtorow$pos[[10]] <- 8
addtorow$pos[[11]] <- 10
addtorow$command <- c (Header1, Header3, Header4, Header4b, Header5
                       , Header6, Header6b, Header5, Header7, Header7b, Header5)
print (xtable (cbind (rows, tabRob1202)
               , align=c("l","l","c","c","c","c","c")
               , digits=3
               , caption="\\bf Yearly Data (2): No FEs, SEs Clustered by Country"
               , label="T:robustYearMain02")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.41) Table A40: Yearly Data (3): No FEs, SEs Clusteed by Country + Controls

scaling <- 18*3

coefRob1203a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1203a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1203 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1203a), 3)), ncol=3))
pvalRob1203 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1203a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1203a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=3)
tabRob1203 <- rbind (coefRob1203, pvalRob1203)[c (1, 1+nrow (coefRob1203)) + sort (rep ((0:(nrow (coefRob1203)-1)), 2)),]
(tabRob1203 <- cbind (tabRob1203[,1]
                      , rep ("", nrow (tabRob1203))
                      , tabRob1203[,2]
                      , rep ("", nrow (tabRob1203))
                      , tabRob1203[,3]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{1}{c}{\\textbf{At Least One}} & & \\multicolumn{1}{c}{\\textbf{Total Number of}} & & \\multicolumn{1}{c}{\\textbf{Size of Largest}} \\\\ \\cmidrule{2-2} \\cmidrule{4-4} \\cmidrule{6-6} \n")
Header2 <- paste (" & \\multicolumn{5}{c}{\\textbf{Neighbor Protest(s) in Previous Year}} \\\\ \\midrule \n")
Header4 <- paste (" & \\multicolumn{5}{c}{\\textbf{A. 50km Neighbors}} \\\\ \n", sep="")
Header4b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsCont01main[1]), " countries; ", sprintf ("%.0f", round (nyearsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{6}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{B. 800km Neighbors}} \\\\ \n", sep="")
Header6b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsN80001main[1]), " countries; ", sprintf ("%.0f", round (nyearsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{C. UN Region Neighbors}} \\\\ \n", sep="")
Header7b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsUNReg01main[1]), " countries; ", sprintf ("%.0f", round (nyearsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 4
addtorow$pos[[8]] <- 6
addtorow$pos[[9]] <- 8
addtorow$pos[[10]] <- 8
addtorow$pos[[11]] <- 10
addtorow$command <- c (Header1, Header3, Header4, Header4b, Header5
                       , Header6, Header6b, Header5, Header7, Header7b, Header5)
print (xtable (cbind (rows, tabRob1203)
               , align=c("l","l","c","c","c","c","c")
               , digits=3
               , caption="\\bf Yearly Data (3): No FEs but SEs Clustered by Country $+$ Controls"
               , label="T:robustYearMain03")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.42) Table A41: Yearly Data (4): FEs by Country and SEs Clustered by Country

scaling <- 18*4

coefRob1204a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob1204a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob1204 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1204a), 3)), ncol=3))
pvalRob1204 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1204a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1204a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=3)
tabRob1204 <- rbind (coefRob1204, pvalRob1204)[c (1, 1+nrow (coefRob1204)) + sort (rep ((0:(nrow (coefRob1204)-1)), 2)),]
(tabRob1204 <- cbind (tabRob1204[,1]
                      , rep ("", nrow (tabRob1204))
                      , tabRob1204[,2]
                      , rep ("", nrow (tabRob1204))
                      , tabRob1204[,3]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{1}{c}{\\textbf{At Least One}} & & \\multicolumn{1}{c}{\\textbf{Total Number of}} & & \\multicolumn{1}{c}{\\textbf{Size of Largest}} \\\\ \\cmidrule{2-2} \\cmidrule{4-4} \\cmidrule{6-6} \n")
Header2 <- paste (" & \\multicolumn{5}{c}{\\textbf{Neighbor Protest(s) in Previous Year}} \\\\ \\midrule \n")
Header4 <- paste (" & \\multicolumn{5}{c}{\\textbf{A. 50km Neighbors}} \\\\ \n", sep="")
Header4b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsCont01main[1]), " countries; ", sprintf ("%.0f", round (nyearsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{6}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{B. 800km Neighbors}} \\\\ \n", sep="")
Header6b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsN80001main[1]), " countries; ", sprintf ("%.0f", round (nyearsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{C. UN Region Neighbors}} \\\\ \n", sep="")
Header7b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsUNReg01main[1]), " countries; ", sprintf ("%.0f", round (nyearsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 4
addtorow$pos[[8]] <- 6
addtorow$pos[[9]] <- 8
addtorow$pos[[10]] <- 8
addtorow$pos[[11]] <- 10
addtorow$command <- c (Header1, Header3, Header4, Header4b, Header5
                       , Header6, Header6b, Header5, Header7, Header7b, Header5)
print (xtable (cbind (rows, tabRob1204)
               , align=c("l","l","c","c","c","c","c")
               , digits=3
               , caption="\\bf Yearly Data (4): FEs by cCuntry and SEs Clustered by Country"
               , label="T:robustYearMain04")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.43) Table A42: Yearly Data (5): FEs by Country and SEs Clustered by Country + Controls

scaling <- 18*5

coefRob1205a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob1205a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob1205 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1205a), 3)), ncol=3))
pvalRob1205 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1205a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1205a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=3)
tabRob1205 <- rbind (coefRob1205, pvalRob1205)[c (1, 1+nrow (coefRob1205)) + sort (rep ((0:(nrow (coefRob1205)-1)), 2)),]
(tabRob1205 <- cbind (tabRob1205[,1]
                      , rep ("", nrow (tabRob1205))
                      , tabRob1205[,2]
                      , rep ("", nrow (tabRob1205))
                      , tabRob1205[,3]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{1}{c}{\\textbf{At Least One}} & & \\multicolumn{1}{c}{\\textbf{Total Number of}} & & \\multicolumn{1}{c}{\\textbf{Size of Largest}} \\\\ \\cmidrule{2-2} \\cmidrule{4-4} \\cmidrule{6-6} \n")
Header2 <- paste (" & \\multicolumn{5}{c}{\\textbf{Neighbor Protest(s) in Previous Year}} \\\\ \\midrule \n")
Header4 <- paste (" & \\multicolumn{5}{c}{\\textbf{A. 50km Neighbors}} \\\\ \n", sep="")
Header4b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsCont01main[1]), " countries; ", sprintf ("%.0f", round (nyearsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{6}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{B. 800km Neighbors}} \\\\ \n", sep="")
Header6b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsN80001main[1]), " countries; ", sprintf ("%.0f", round (nyearsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{C. UN Region Neighbors}} \\\\ \n", sep="")
Header7b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsUNReg01main[1]), " countries; ", sprintf ("%.0f", round (nyearsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 4
addtorow$pos[[8]] <- 6
addtorow$pos[[9]] <- 8
addtorow$pos[[10]] <- 8
addtorow$pos[[11]] <- 10
addtorow$command <- c (Header1, Header3, Header4, Header4b, Header5
                       , Header6, Header6b, Header5, Header7, Header7b, Header5)
print (xtable (cbind (rows, tabRob1205)
               , align=c("l","l","c","c","c","c","c")
               , digits=3
               , caption="\\bf Yearly Data (5): FEs by Country and SEs Clustered by Country $+$ Controls"
               , label="T:robustYearMain05")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.44) Table A43: Yearly Data (6): Logit with REs by Country

scaling <- 18*8

coefRob1206a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1206a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob12y01[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob12y01[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob12y01[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1206 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1206a), 3)), ncol=3))
pvalRob1206 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1206a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1206a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=3)
tabRob1206 <- rbind (coefRob1206, pvalRob1206)[c (1, 1+nrow (coefRob1206)) + sort (rep ((0:(nrow (coefRob1206)-1)), 2)),]
(tabRob1206 <- cbind (tabRob1206[,1]
                      , rep ("", nrow (tabRob1206))
                      , tabRob1206[,2]
                      , rep ("", nrow (tabRob1206))
                      , tabRob1206[,3]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{1}{c}{\\textbf{At Least One}} & & \\multicolumn{1}{c}{\\textbf{Total Number of}} & & \\multicolumn{1}{c}{\\textbf{Size of Largest}} \\\\ \\cmidrule{2-2} \\cmidrule{4-4} \\cmidrule{6-6} \n")
Header2 <- paste (" & \\multicolumn{5}{c}{\\textbf{Neighbor Protest(s) in Previous Year}} \\\\ \\midrule \n")
Header4 <- paste (" & \\multicolumn{5}{c}{\\textbf{A. 50km Neighbors}} \\\\ \n", sep="")
Header4b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsCont01main[1]), " countries; ", sprintf ("%.0f", round (nyearsCont01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{6}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{B. 800km Neighbors}} \\\\ \n", sep="")
Header6b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsN80001main[1]), " countries; ", sprintf ("%.0f", round (nyearsN80001main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule & \\multicolumn{5}{c}{\\textbf{C. UN Region Neighbors}} \\\\ \n", sep="")
Header7b <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{5}{c}{(", sprintf ("%.0f", nyearsUNReg01main[1]), " countries; ", sprintf ("%.0f", round (nyearsUNReg01main[3], 0)), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 4
addtorow$pos[[8]] <- 6
addtorow$pos[[9]] <- 8
addtorow$pos[[10]] <- 8
addtorow$pos[[11]] <- 10
addtorow$command <- c (Header1, Header3, Header4, Header4b, Header5
                       , Header6, Header6b, Header5, Header7, Header7b, Header5)
print (xtable (cbind (rows, tabRob1206)
               , align=c("l","l","c","c","c","c","c")
               , digits=3
               , caption="\\bf Yearly Data (6): Logit with REs by Country"
               , label="T:robustYearMain06")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.45) Table A44: Protest Size, Conditional on Neighbor Protest

# protest-only dataset
bProt <- base[base$inplace==0 & base$pbega==1,]

# creating the table
(pSizes <- cbind (
  
  ## 50km neighbors
  
  # 45-day lag
  c (mean (bProt$size, na.rm=T), mean (bProt[!is.na (bProt$n50_d45_wequal_any) & bProt$n50_d45_wequal_any==1,]$size, na.rm=T), mean (bProt[!is.na (bProt$n50_d45_wequal_any) & bProt$n50_d45_wequal_any==0,]$size, na.rm=T)
     , median (bProt$size, na.rm=T), median (bProt[!is.na (bProt$n50_d45_wequal_any) & bProt$n50_d45_wequal_any==1,]$size, na.rm=T), median (bProt[!is.na (bProt$n50_d45_wequal_any) & bProt$n50_d45_wequal_any==0,]$size, na.rm=T)
     , getmode (bProt$size), getmode (bProt[!is.na (bProt$n50_d45_wequal_any) & bProt$n50_d45_wequal_any==1,]$size), getmode (bProt[!is.na (bProt$n50_d45_wequal_any) & bProt$n50_d45_wequal_any==0,]$size))
  
  # 90-day lag
  , c (mean (bProt$size, na.rm=T), mean (bProt[!is.na (bProt$n50_d90_wequal_any) & bProt$n50_d90_wequal_any==1,]$size, na.rm=T), mean (bProt[!is.na (bProt$n50_d90_wequal_any) & bProt$n50_d90_wequal_any==0,]$size, na.rm=T)
       , median (bProt$size, na.rm=T), median (bProt[!is.na (bProt$n50_d90_wequal_any) & bProt$n50_d90_wequal_any==1,]$size, na.rm=T), median (bProt[!is.na (bProt$n50_d90_wequal_any) & bProt$n50_d90_wequal_any==0,]$size, na.rm=T)
       , getmode (bProt$size), getmode (bProt[!is.na (bProt$n50_d90_wequal_any) & bProt$n50_d90_wequal_any==1,]$size), getmode (bProt[!is.na (bProt$n50_d90_wequal_any) & bProt$n50_d90_wequal_any==0,]$size))
  
  # 120-day lag
  , c (mean (bProt$size, na.rm=T), mean (bProt[!is.na (bProt$n50_d120_wequal_any) & bProt$n50_d120_wequal_any==1,]$size, na.rm=T), mean (bProt[!is.na (bProt$n50_d120_wequal_any) & bProt$n50_d120_wequal_any==0,]$size, na.rm=T)
       , median (bProt$size, na.rm=T), median (bProt[!is.na (bProt$n50_d120_wequal_any) & bProt$n50_d120_wequal_any==1,]$size, na.rm=T), median (bProt[!is.na (bProt$n50_d120_wequal_any) & bProt$n50_d120_wequal_any==0,]$size, na.rm=T)
       , getmode (bProt$size), getmode (bProt[!is.na (bProt$n50_d120_wequal_any) & bProt$n50_d120_wequal_any==1,]$size), getmode (bProt[!is.na (bProt$n50_d120_wequal_any) & bProt$n50_d120_wequal_any==0,]$size))
  
  
  ## 800km neighbors
  
  # 45-day lag
  , c (mean (bProt$size, na.rm=T), mean (bProt[!is.na (bProt$n800_d45_wequal_any) & bProt$n800_d45_wequal_any==1,]$size, na.rm=T), mean (bProt[!is.na (bProt$n800_d45_wequal_any) & bProt$n800_d45_wequal_any==0,]$size, na.rm=T)
       , median (bProt$size, na.rm=T), median (bProt[!is.na (bProt$n800_d45_wequal_any) & bProt$n800_d45_wequal_any==1,]$size, na.rm=T), median (bProt[!is.na (bProt$n800_d45_wequal_any) & bProt$n800_d45_wequal_any==0,]$size, na.rm=T)
       , getmode (bProt$size), getmode (bProt[!is.na (bProt$n800_d45_wequal_any) & bProt$n800_d45_wequal_any==1,]$size), getmode (bProt[!is.na (bProt$n800_d45_wequal_any) & bProt$n800_d45_wequal_any==0,]$size))
  
  # 90-day lag
  , c (mean (bProt$size, na.rm=T), mean (bProt[!is.na (bProt$n800_d90_wequal_any) & bProt$n800_d90_wequal_any==1,]$size, na.rm=T), mean (bProt[!is.na (bProt$n800_d90_wequal_any) & bProt$n800_d90_wequal_any==0,]$size, na.rm=T)
       , median (bProt$size, na.rm=T), median (bProt[!is.na (bProt$n800_d90_wequal_any) & bProt$n800_d90_wequal_any==1,]$size, na.rm=T), median (bProt[!is.na (bProt$n800_d90_wequal_any) & bProt$n800_d90_wequal_any==0,]$size, na.rm=T)
       , getmode (bProt$size), getmode (bProt[!is.na (bProt$n800_d90_wequal_any) & bProt$n800_d90_wequal_any==1,]$size), getmode (bProt[!is.na (bProt$n800_d90_wequal_any) & bProt$n800_d90_wequal_any==0,]$size))
  
  # 120-day lag
  , c (mean (bProt$size, na.rm=T), mean (bProt[!is.na (bProt$n800_d120_wequal_any) & bProt$n800_d120_wequal_any==1,]$size, na.rm=T), mean (bProt[!is.na (bProt$n800_d120_wequal_any) & bProt$n800_d120_wequal_any==0,]$size, na.rm=T)
       , median (bProt$size, na.rm=T), median (bProt[!is.na (bProt$n800_d120_wequal_any) & bProt$n800_d120_wequal_any==1,]$size, na.rm=T), median (bProt[!is.na (bProt$n800_d120_wequal_any) & bProt$n800_d120_wequal_any==0,]$size, na.rm=T)
       , getmode (bProt$size), getmode (bProt[!is.na (bProt$n800_d120_wequal_any) & bProt$n800_d120_wequal_any==1,]$size), getmode (bProt[!is.na (bProt$n800_d120_wequal_any) & bProt$n800_d120_wequal_any==0,]$size))
))

# statistically significant differences?
t.test (x=bProt[!is.na (bProt$n50_d45_wequal_any) & bProt$n50_d45_wequal_any==1,]$size, y=bProt[!is.na (bProt$n50_d45_wequal_any) & bProt$n50_d45_wequal_any==0,]$size)
t.test (x=bProt[!is.na (bProt$n50_d90_wequal_any) & bProt$n50_d90_wequal_any==1,]$size, y=bProt[!is.na (bProt$n50_d90_wequal_any) & bProt$n50_d90_wequal_any==0,]$size)
t.test (x=bProt[!is.na (bProt$n50_d120_wequal_any) & bProt$n50_d120_wequal_any==1,]$size, y=bProt[!is.na (bProt$n50_d120_wequal_any) & bProt$n50_d120_wequal_any==0,]$size)

t.test (x=bProt[!is.na (bProt$n800_d45_wequal_any) & bProt$n800_d45_wequal_any==1,]$size, y=bProt[!is.na (bProt$n800_d45_wequal_any) & bProt$n800_d45_wequal_any==0,]$size)
t.test (x=bProt[!is.na (bProt$n800_d90_wequal_any) & bProt$n800_d90_wequal_any==1,]$size, y=bProt[!is.na (bProt$n800_d90_wequal_any) & bProt$n800_d90_wequal_any==0,]$size)
t.test (x=bProt[!is.na (bProt$n800_d120_wequal_any) & bProt$n800_d120_wequal_any==1,]$size, y=bProt[!is.na (bProt$n800_d120_wequal_any) & bProt$n800_d120_wequal_any==0,]$size)

# building the table
pSizes2 <- matrix (sprintf ("%.2f", round (pSizes, 2)), ncol=6)
pSizes2 <- cbind (pSizes2[,1:3], rep ("", nrow (pSizes2)), pSizes2[,4:6])

rows <- rep (c ("\\emph{Full sample}", "\\emph{Protest in neighbor}", "\\emph{No protest in neighbor}"), 3)

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{$50$km}} & & \\multicolumn{3}{c}{\\textbf{$800$km}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{protest in last}} & & \\multicolumn{3}{c}{\\textbf{protest in last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n")
Header3 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Mean}}} & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Median}}} \\\\ \\midrule \n")
Header5 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Mode}}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 3
addtorow$pos[[5]] <- 6
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5)
print (xtable (cbind (rows, pSizes2)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Protest Size, Conditional on Neighbor Protest"
               , label="T:psize")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.46) Table A45: Main Results wit the 2007 Venezuelan Protests

# getting the coefficients
coefRob16a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mContRob16[1:9], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob16[10:18], FUN=function (x) x[1,1]))
  
  # 800-km neighbors
  , unlist (lapply (mN800Rob16[1:9], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob16[10:18], FUN=function (x) x[1,1]))
  
  # UN Region neighbors
  , unlist (lapply (mUNRegRob16[1:9], FUN=function (x) x[1,1]))
  , unlist (lapply (mUNRegRob16[10:18], FUN=function (x) x[1,1]))
)
pvalRob16a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mContRob16[1:9], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob16[10:18], FUN=function (x) x[1,4]))
  
  # 800-km neighbors
  , unlist (lapply (mN800Rob16[1:9], FUN=function (x) x[1,4]))
  , unlist (lapply (mN800Rob16[10:18], FUN=function (x) x[1,4]))
  
  # UN Region neighbors
  , unlist (lapply (mUNRegRob16[1:9], FUN=function (x) x[1,4]))
  , unlist (lapply (mUNRegRob16[10:18], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob16 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob16a), 3)), ncol=9))
pvalRob16 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob16a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob16a<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob16), ncol=9)
tabRob16 <- rbind (coefRob16, pvalRob16)[c (1, 1+nrow (coefRob16)) + sort (rep ((0:(nrow (coefRob16)-1)), 2)),]
(tabRob16 <- cbind (tabRob16[,1:3]
                    , rep ("", nrow (tabRob16))
                    , tabRob16[,4:6]
                    , rep ("", nrow (tabRob16))
                    , tabRob16[,7:9]))

# building the table
rows <- c (rep (c ("\\emph{Neighbor Democracy Protest}", ""), 6))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsContRob16[1]), " countries; ", sprintf ("%.0f", nobsContRob16[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsContRob16[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN800Rob16[1]), " countries; ", sprintf ("%.0f", nobsN800Rob16[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsN800Rob16[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nobsUNRegRob16[1]), " countries; ", sprintf ("%.0f", nobsUNRegRob16[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nobsUNRegRob16[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob16)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Main Results with the 2007 Venezuelan Protests"
               , label="T:resultsVenezuela")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.47) Table A46: Target Anti-regime Contention (GDELT Data), 1989-2011

# getting the coefficients
coefRob03a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mContRob02[1:6], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob02[7:12], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob02[13:18], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob02[19:24], FUN=function (x) x[1,1]))
  
  # 800-km neighbors
  , unlist (lapply (mN800Rob02[1:6], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob02[7:12], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob02[13:18], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob02[19:24], FUN=function (x) x[1,1]))
)
pvalRob03a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mContRob02[1:6], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob02[7:12], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob02[13:18], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob02[19:24], FUN=function (x) x[1,4]))
  
  # 800-km neighbors
  , unlist (lapply (mN800Rob02[1:6], FUN=function (x) x[1,4]))
  , unlist (lapply (mN800Rob02[7:12], FUN=function (x) x[1,4]))
  , unlist (lapply (mN800Rob02[13:18], FUN=function (x) x[1,4]))
  , unlist (lapply (mN800Rob02[19:24], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob03 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob03a), 3)), ncol=6))
pvalRob03 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob03a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob03a<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob03), ncol=6)
tabRob03 <- rbind (coefRob03, pvalRob03)[c (1, 1+nrow (coefRob03)) + sort (rep ((0:(nrow (coefRob03)-1)), 2)),]
(tabRob03 <- cbind (tabRob03[,1:3]
                    , rep ("", nrow (tabRob03))
                    , tabRob03[,4:6]))

# building the table
rows <- c (rep (c ("\\emph{Neighbor Democracy Protest}", ""), 8))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n")
Header3 <- paste ("& \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{8}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsContRob02.low[1]), "-", sprintf ("%.0f", nobsContRob02.high[1]), " countries; ", sprintf ("%.0f", nobsContRob02.low[2]), "-", sprintf ("%.0f", nobsContRob02.high[2]), " country-years; ", sprintf ("%.2f", round (nobsContRob02.low[3]/1000000, 2)), "-", sprintf ("%.2f", round (nobsContRob02.high[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n", sep="")
Header6 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline} (2011 excluded)}} \\\\ \\midrule \n", sep="")
Header7 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline $+$ Election} (2011 excluded)}} \\\\ \\midrule \n", sep="")
Header8 <- paste ("\\toprule \\multicolumn{8}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN800Rob02.low[1]), "-", sprintf ("%.0f", nobsN800Rob02.high[1]), " countries; ", sprintf ("%.0f", nobsN800Rob02.low[2]), "-", sprintf ("%.0f", nobsN800Rob02.high[2]), " country-years; ", sprintf ("%.2f", round (nobsN800Rob02.low[3]/1000000, 2)), "-", sprintf ("%.2f", round (nobsN800Rob02.high[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$pos[[10]] <- 12
addtorow$pos[[11]] <- 14
addtorow$command <- c (Header1, Header2, Header3, Header4
                       , Header5, Header6, Header7
                       , Header8, Header5, Header6, Header7)
print (xtable (cbind (rows, tabRob03)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Target Anti-regime Contention (GDELT Data), 1989-2011"
               , label="T:resultsGdelt")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.48) Table A47: Target Anti-regime Contention (SPEED Data), 1989-2005

# getting the coefficients
coefRob04a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mContRob03[1:6], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob03[7:12], FUN=function (x) x[1,1]))
  
  # 800-km neighbors
  , unlist (lapply (mN800Rob03[1:6], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob03[7:12], FUN=function (x) x[1,1]))
)
pvalRob04a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mContRob03[1:6], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob03[7:12], FUN=function (x) x[1,4]))
  
  # 800-km neighbors
  , unlist (lapply (mN800Rob03[1:6], FUN=function (x) x[1,4]))
  , unlist (lapply (mN800Rob03[7:12], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob04 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob04a), 3)), ncol=6))
pvalRob04 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob04a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob04a<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob04), ncol=6)
tabRob04 <- rbind (coefRob04, pvalRob04)[c (1, 1+nrow (coefRob04)) + sort (rep ((0:(nrow (coefRob04)-1)), 2)),]
(tabRob04 <- cbind (tabRob04[,1:3]
                    , rep ("", nrow (tabRob04))
                    , tabRob04[,4:6]))

# building the table
rows <- c (rep (c ("\\emph{Neighbor Democracy Protest}", ""), 4))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n")
Header3 <- paste ("& \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{8}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsContRob03[1]), " countries; ", sprintf ("%.0f", nobsContRob03[2]), " country-years; $\\approx$", sprintf ("%.2f", round (nobsContRob03[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n", sep="")
Header6 <- paste ("\\toprule \\multicolumn{8}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN800Rob03[1]), " countries; ", sprintf ("%.0f", nobsN800Rob03[2]), " country-years; $\\approx$", sprintf ("%.2f", round (nobsN800Rob03[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$command <- c (Header1, Header2, Header3, Header4
                       , Header5
                       , Header6, Header5)
print (xtable (cbind (rows, tabRob04)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Target Anti-regime Contention (SPEED Data), 1989-2005"
               , label="T:resultsSpeed")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.49) Table A48: Target Anti-government Contention (Mass Mobilization Data), 1990-2011

# getting the coefficients
coefRob05a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mContRob04[1:6], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob04[10:15], FUN=function (x) x[1,1]))
  
  # 800-km neighbors
  , unlist (lapply (mN800Rob04[1:6], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob04[10:15], FUN=function (x) x[1,1]))
)
pvalRob05a <- rbind (
  
  # contiguous neighbors
  unlist (lapply (mContRob04[1:6], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob04[10:15], FUN=function (x) x[1,4]))
  
  # 800-km neighbors
  , unlist (lapply (mN800Rob04[1:6], FUN=function (x) x[1,4]))
  , unlist (lapply (mN800Rob04[10:15], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob05 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob05a), 3)), ncol=6))
pvalRob05 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob05a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob05a<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob05), ncol=6)
tabRob05 <- rbind (coefRob05, pvalRob05)[c (1, 1+nrow (coefRob05)) + sort (rep ((0:(nrow (coefRob05)-1)), 2)),]
(tabRob05 <- cbind (tabRob05[,1:3]
                    , rep ("", nrow (tabRob05))
                    , tabRob05[,4:6]))

# building the table
rows <- c (rep (c ("\\emph{Neighbor Democracy Protest}", ""), 4))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n")
Header3 <- paste ("& \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{8}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nobsContRob04[1]), " countries; ", sprintf ("%.0f", nobsContRob04[2]), " country-years; $\\approx$", sprintf ("%.2f", round (nobsContRob04[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n", sep="")
Header6 <- paste ("\\toprule \\multicolumn{8}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nobsN800Rob04[1]), " countries; ", sprintf ("%.0f", nobsN800Rob04[2]), " country-years; $\\approx$", sprintf ("%.2f", round (nobsN800Rob04[3]/1000000, 2)), "m obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$command <- c (Header1, Header2, Header3, Header4
                       , Header5
                       , Header6, Header5)
print (xtable (cbind (rows, tabRob05)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Target Anti-government Contention (Mass Mobilization Data), 1990-2011"
               , label="T:resultsMMD")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.50) Table A49: Democracy Protests in 1989

base89[base89$pbega==1,c ("country", "date", "region")] ## only half of the protests (8/15) took place in Eastern Europe:
# 187693        Bulgaria 1989-11-03     East Europe
# 187708        Bulgaria 1989-11-18     East Europe
# 271308           China 1989-04-18       East Asia
# 352218  Czechoslovakia 1989-01-15     East Europe
# 352499  Czechoslovakia 1989-10-28     East Europe
# 379041    East Germany 1989-09-04     East Europe
# 379042    East Germany 1989-10-18     East Europe
# 551064         Hungary 1989-03-15     East Europe
# 676881          Kuwait 1989-12-04       West Asia
# 764103      Madagascar 1989-04-19     East Africa
# 846549        Mongolia 1989-12-10       East Asia
# 873319   Myanmar/Burma 1989-07-06 South-East Asia
# 1039433        Romania 1989-12-16     East Europe
# 1143159   South Africa 1989-08-16    South Africa
# 1228371         Taiwan 1989-12-02       East Asia   # note that the other 7 protests took place in several countries that were not neighbors between them --or were neighbors but the protests did not spread between them in the shrot term (e.g. in China the Tiananmen Square protests began in April, but the protests in Mongolia and Taiwan only began in December)


### (2.51) Table A50: Data Overview: 1989
t89.all <- rbind (
  cbind (rbind (t89Cont30[2,], t89Cont45[2,], t89Cont90[2,], t89Cont120[2,])
         , rbind (t89N800.30[2,], t89N800.45[2,], t89N800.90[2,], t89N800.120[2,])
         , rbind (t89UNReg30[2,], t89UNReg45[2,], t89UNReg90[2,], t89UNReg120[2,]))
  , cbind (rbind (t89Cont30abs[2,], t89Cont45abs[2,], t89Cont90abs[2,], t89Cont120abs[2,])
           , rbind (t89N800.30abs[2,], t89N800.45abs[2,], t89N800.90abs[2,], t89N800.120abs[2,])
           , rbind (t89UNReg30abs[2,], t89UNReg45abs[2,], t89UNReg90abs[2,], t89UNReg120abs[2,])))
t89.all2 <- as.data.frame (rbind (
  matrix (paste (sprintf ("%.3f", round (t89.all[1:4,], 3)), "\\%", sep=""), ncol=6)
  , matrix (paste ("[", sprintf ("%.0f", round (t89.all[5:8,], 0)), "]", sep=""), ncol=6)))

# adding the asterisks to indicate statistically significant differences
t89.all2$V2 <- as.character (t89.all2$V2)
t89.all2$V4 <- as.character (t89.all2$V4)
t89.all2$V6 <- as.character (t89.all2$V6)
cbind (c (tval89Cont30$p.value, tval89Cont45$p.value, tval89Cont90$p.value, tval89Cont120$p.value)
       , c (tval89N800.30$p.value, tval89N800.45$p.value, tval89N800.90$p.value, tval89N800.120$p.value)
       , c (tval89UNReg30$p.value, tval89UNReg45$p.value, tval89UNReg90$p.value, tval89UNReg120$p.value))<0.05 ## one case
t89.all2[1,6] <- paste (t89.all2[1,6], "*", sep="")
t89.all2[3,6] <- paste (t89.all2[3,6], "*", sep="")

odd <- seq (1, nrow (t89.all), by=2); even <- seq (2, nrow (t89.all), by=2)
row.names (t89.all2) <- as.numeric (c (odd, even))
t89.all2 <- as.matrix (t89.all2[order (as.numeric (row.names (t89.all2))),])
(t89.all3 <- cbind (
  t89.all2[,1:2]
  , rep ("", nrow(t89.all))
  , t89.all2[,3:4]
  , rep ("", nrow(t89.all))
  , t89.all2[,5:6]))

rows <- c ("\\textbf{30 days}", "", "\\textbf{45 days}", "", "\\textbf{90 days}", "", "\\textbf{120 days}", "")

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\textbf{$50$km}} & & \\multicolumn{2}{c}{\\textbf{$800$km}} & & \\multicolumn{2}{c}{\\textbf{UN region}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} \\\\ \\cmidrule{2-3} \\cmidrule{5-6} \\cmidrule{8-9} \n")
Header3 <- paste ("\\multicolumn{1}{l}{} & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} \\\\ \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{protest in last...}} & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$command <- c (Header1, Header2, Header3, Header4)
print (xtable (cbind (rows, t89.all3)
               , align=c("c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Data Overview: 1989"
               , label="T:overview1989")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.52) Table A52: Gorbachev's Speech and the 1989 Protests

coefRob1204a <- cbind (
  c (sum.cont89d45a$coef[c (2:5,1),1], sum.cont89d45b$coef[c (2:5,1),1])
  , c (sum.cont89d90a$coef[c (2:5,1),1], sum.cont89d90b$coef[c (2:5,1),1])
  , c (sum.cont89d120a$coef[c (2:5,1),1], sum.cont89d120b$coef[c (2:5,1),1])
  
  , c (sum.n80089d45a$coef[c (2:5,1),1], sum.n80089d45b$coef[c (2:5,1),1])
  , c (sum.n80089d90a$coef[c (2:5,1),1], sum.n80089d90b$coef[c (2:5,1),1])
  , c (sum.n80089d120a$coef[c (2:5,1),1], sum.n80089d120b$coef[c (2:5,1),1])
  
  , c (sum.unreg89d45a$coef[c (2:5,1),1], sum.unreg89d45b$coef[c (2:5,1),1])
  , c (sum.unreg89d90a$coef[c (2:5,1),1], sum.unreg89d90b$coef[c (2:5,1),1])
  , c (sum.unreg89d120a$coef[c (2:5,1),1], sum.unreg89d120b$coef[c (2:5,1),1])
)
seRob1204a <- cbind (
  c (sum.cont89d45a$coef[c (2:5,1),2], sum.cont89d45b$coef[c (2:5,1),2])
  , c (sum.cont89d90a$coef[c (2:5,1),2], sum.cont89d90b$coef[c (2:5,1),2])
  , c (sum.cont89d120a$coef[c (2:5,1),2], sum.cont89d120b$coef[c (2:5,1),2])
  
  , c (sum.n80089d45a$coef[c (2:5,1),2], sum.n80089d45b$coef[c (2:5,1),2])
  , c (sum.n80089d90a$coef[c (2:5,1),2], sum.n80089d90b$coef[c (2:5,1),2])
  , c (sum.n80089d120a$coef[c (2:5,1),2], sum.n80089d120b$coef[c (2:5,1),2])
  
  , c (sum.unreg89d45a$coef[c (2:5,1),2], sum.unreg89d45b$coef[c (2:5,1),2])
  , c (sum.unreg89d90a$coef[c (2:5,1),2], sum.unreg89d90b$coef[c (2:5,1),2])
  , c (sum.unreg89d120a$coef[c (2:5,1),2], sum.unreg89d120b$coef[c (2:5,1),2])
)
pvalRob1204a <- cbind (
  c (sum.cont89d45a$coef[c (2:5,1),4], sum.cont89d45b$coef[c (2:5,1),4])
  , c (sum.cont89d90a$coef[c (2:5,1),4], sum.cont89d90b$coef[c (2:5,1),4])
  , c (sum.cont89d120a$coef[c (2:5,1),4], sum.cont89d120b$coef[c (2:5,1),4])
  
  , c (sum.n80089d45a$coef[c (2:5,1),4], sum.n80089d45b$coef[c (2:5,1),4])
  , c (sum.n80089d90a$coef[c (2:5,1),4], sum.n80089d90b$coef[c (2:5,1),4])
  , c (sum.n80089d120a$coef[c (2:5,1),4], sum.n80089d120b$coef[c (2:5,1),4])
  
  , c (sum.unreg89d45a$coef[c (2:5,1),4], sum.unreg89d45b$coef[c (2:5,1),4])
  , c (sum.unreg89d90a$coef[c (2:5,1),4], sum.unreg89d90b$coef[c (2:5,1),4])
  , c (sum.unreg89d120a$coef[c (2:5,1),4], sum.unreg89d120b$coef[c (2:5,1),4])
)

# retrieving the estimates
coefRob1204 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1204a), 3)), ncol=9))
seRob1204 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (seRob1204a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1204a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=10, ncol=9)
tabRob1204 <- rbind (coefRob1204, seRob1204)[c (1, 1+nrow (coefRob1204)) + sort (rep ((0:(nrow (coefRob1204)-1)), 2)),]
(tabRob1204 <- cbind (tabRob1204[,1:3]
                      , rep ("", nrow (tabRob1204))
                      , tabRob1204[,4:6]
                      , rep ("", nrow (tabRob1204))
                      , tabRob1204[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Time since Speech (in weeks)}", ""
           , "\\emph{East Europe}", ""
           , "\\emph{Time since Speech (in weeks)}", "~~~~~$\\times$ \\emph{East Europe}"
           , "\\emph{Intercept}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Within (45/90/120) days of speech}", ""
           , "\\emph{East Europe}", ""
           , "\\emph{Within (45/90/120) days of speech}", "~~~~~$\\times$ \\emph{East Europe}"
           , "\\emph{Intercept}", "")

(nRob1204 <- cbind (
  c (sum (base89[!is.na (base89$n50_d45_wequal_any),]$pbega), length (unique (base89[!is.na (base89$n50_d45_wequal_any),]$country)), sum.cont89d45a$N)
  , c (sum (base89[!is.na (base89$n50_d90_wequal_any),]$pbega), length (unique (base89[!is.na (base89$n50_d90_wequal_any),]$country)), sum.cont89d90a$N)
  , c (sum (base89[!is.na (base89$n50_d120_wequal_any),]$pbega), length (unique (base89[!is.na (base89$n50_d120_wequal_any),]$country)), sum.cont89d120a$N)
  , c (sum (base89[!is.na (base89$n800_d45_wequal_any),]$pbega), length (unique (base89[!is.na (base89$n800_d45_wequal_any),]$country)), sum.cont89d45a$N)
  , c (sum (base89[!is.na (base89$n800_d90_wequal_any),]$pbega), length (unique (base89[!is.na (base89$n800_d90_wequal_any),]$country)), sum.cont89d90a$N)
  , c (sum (base89[!is.na (base89$n800_d120_wequal_any),]$pbega), length (unique (base89[!is.na (base89$n800_d120_wequal_any),]$country)), sum.cont89d120a$N)
  , c (sum (base89[!is.na (base89$nUN_d45_wequal_any),]$pbega), length (unique (base89[!is.na (base89$nUN_d45_wequal_any),]$country)), sum.cont89d45a$N)
  , c (sum (base89[!is.na (base89$nUN_d90_wequal_any),]$pbega), length (unique (base89[!is.na (base89$nUN_d90_wequal_any),]$country)), sum.cont89d90a$N)
  , c (sum (base89[!is.na (base89$nUN_d120_wequal_any),]$pbega), length (unique (base89[!is.na (base89$nUN_d120_wequal_any),]$country)), sum.cont89d120a$N)
))

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{UN Region Neighbors}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header2 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header3 <- paste ("& \\multicolumn{10}{c}{\\textbf{A. Time after Gorbachev's speech (in weeks)}} \\\\ \\midrule \n", sep="")
Header4 <- paste ("\\midrule & \\multicolumn{10}{c}{\\textbf{B. Dummies for 45/90/120 days after Gorbachev's speech}} \\\\ \\midrule \n", sep="")

add.n01 <- paste ("\\midrule num. protests & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[1,1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[1,2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[1,3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[1,4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[1,5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[1,6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[1,7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[1,8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[1,9],0)), "} \\\\ \n")
add.n02 <- paste ("num. countries & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[2,1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[2,2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[2,3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[2,4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[2,5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[2,6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[2,7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[2,8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[2,9],0)), "} \\\\ \n")
add.n03 <- paste ("num. observations & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[3,1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[3,2],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[3,3],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[3,4],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[3,5],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[3,6],0)), "} & & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[3,7],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[3,8],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nRob1204[3,9],0)), "} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 10
addtorow$pos[[5]] <- 20
addtorow$pos[[6]] <- 20
addtorow$pos[[7]] <- 20
addtorow$command <- c (Header1, Header2, Header3
                       , Header4
                       , add.n01, add.n02, add.n03)
print (xtable (cbind (rows, tabRob1204)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Gorbachev's Speech and the 1989 Protests"
               , label="T:speech89")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.53) Table A53: Democracy Protests in 2011

base11[base11$pbega==1,c ("country", "date", "region")] ## only a quarter of the protests (6/25) took place in what is commonly know as the MENA:
# 16344       Albania 2011-01-21    South Europe
# 88980       Bahrain 2011-02-14       West Asia
# 137896        Benin 2011-03-24     West Africa
# 228889     Cameroon 2011-02-23   Middle Africa
# 279239        China 2011-02-20       East Asia
# 370079     Djibouti 2011-01-28     East Africa
# 395467        Egypt 2011-01-25    North Africa
# 468452        Gabon 2011-01-27   Middle Africa
# 475840      Georgia 2011-05-21       West Asia
# 526302       Guinea 2011-09-27     West Africa
# 534621       Guyana 2011-12-01   South America
# 634052  Ivory Coast 2011-03-01     West Africa
# 691979   Kyrgyzstan 2011-10-31    Central Asia
# 732887      Liberia 2011-11-07     West Africa
# 788527     Malaysia 2011-07-09 South-East Asia
# 864587      Morocco 2011-02-20    North Africa
# 864664      Morocco 2011-09-18    North Africa
# 922298    Nicaragua 2011-11-07 Central America
# 938452      Nigeria 2011-04-17     West Africa
# 1054748      Russia 2011-12-04     East Europe
# 1202577   Swaziland 2011-04-12    South Africa
# 1202722   Swaziland 2011-09-05    South Africa
# 1296780     Tunisia 2011-04-16    North Africa
# 1320849      Uganda 2011-03-09     East Africa
# 1394678       Yemen 2011-01-18       West Asia  # if we added the protests in Tunisia, Syria and Libya, it would be 9/28, just one third


### (2.54) Table A54: Data Overview: 2011

t11.all <- rbind (
  
  # 2011 only, from original data
  cbind (rbind (t11Cont30[2,], t11Cont45[2,], t11Cont90[2,], t11Cont120[2,])
         , rbind (t11N800.30[2,], t11N800.45[2,], t11N800.90[2,], t11N800.120[2,])
         , rbind (t11UNReg30[2,], t11UNReg45[2,], t11UNReg90[2,], t11UNReg120[2,]))
  
  # 2011 only, adding protests in Tunisia, Libya and Syria
  , cbind (rbind (tspringCont30[2,], tspringCont45[2,], tspringCont90[2,], tspringCont120[2,])
           , rbind (tspringN800.30[2,], tspringN800.45[2,], tspringN800.90[2,], tspringN800.120[2,])
           , rbind (tspringUNReg30[2,], tspringUNReg45[2,], tspringUNReg90[2,], tspringUNReg120[2,]))
  
  # 2011 only, MENA sample
  , cbind (rbind (tmena11Cont30[2,], tmena11Cont45[2,], tmena11Cont90[2,], tmena11Cont120[2,])
           , rbind (tmena11N800.30[2,], tmena11N800.45[2,], tmena11N800.90[2,], tmena11N800.120[2,])
           , cbind (rep (NA, 4), rep (NA, 4))  ## cannot do the calculation for the region because there were neighbor democracy protests almost every dar
  )
  
  # 2011 only, from original data
  , cbind (rbind (t11Cont30abs[2,], t11Cont45abs[2,], t11Cont90abs[2,], t11Cont120abs[2,])
           , rbind (t11N800.30abs[2,], t11N800.45abs[2,], t11N800.90abs[2,], t11N800.120abs[2,])
           , rbind (t11UNReg30abs[2,], t11UNReg45abs[2,], t11UNReg90abs[2,], t11UNReg120abs[2,]))
  
  # 2011 only, adding protests in Tunisia, Libya and Syria
  , cbind (rbind (tspringCont30abs[2,], tspringCont45abs[2,], tspringCont90abs[2,], tspringCont120abs[2,])
           , rbind (tspringN800.30abs[2,], tspringN800.45abs[2,], tspringN800.90abs[2,], tspringN800.120abs[2,])
           , rbind (tspringUNReg30abs[2,], tspringUNReg45abs[2,], tspringUNReg90abs[2,], tspringUNReg120abs[2,]))
  
  # 2011 only, MENA sample
  , cbind (rbind (tmena11Cont30abs[2,], tmena11Cont45abs[2,], tmena11Cont90abs[2,], tmena11Cont120abs[2,])
           , rbind (tmena11N800.30abs[2,], tmena11N800.45abs[2,], tmena11N800.90abs[2,], tmena11N800.120abs[2,])
           , cbind (rep (NA, 4), rep (NA, 4))
  )
)
t11.all2 <- as.data.frame (rbind (
  matrix (paste (sprintf ("%.3f", round (t11.all[1:12,], 3)), "\\%", sep=""), ncol=6)
  , matrix (paste ("[", sprintf ("%.0f", round (t11.all[13:24,], 0)), "]", sep=""), ncol=6)))

# adding the asterisks to indicate statistically significant differences
t11.all2$V2 <- as.character (t11.all2$V2)
t11.all2$V4 <- as.character (t11.all2$V4)
t11.all2$V6 <- as.character (t11.all2$V6)
cbind (
  # 2011 only, from original data
  c (tval11Cont30$p.value, tval11Cont45$p.value, tval11Cont90$p.value, tval11Cont120$p.value)
  , c (tval11N800.30$p.value, tval11N800.45$p.value, tval11N800.90$p.value, tval11N800.120$p.value)
  , c (tval11UNReg30$p.value, tval11UNReg45$p.value, tval11UNReg90$p.value, tval11UNReg120$p.value)
  
  # 2011 only, adding protests in Tunisia, Libya and Syria
  , c (tvalSpringCont30$p.value, tvalSpringCont45$p.value, tvalSpringCont90$p.value, tvalSpringCont120$p.value)
  , c (tvalSpringN800.30$p.value, tvalSpringN800.45$p.value, tvalSpringN800.90$p.value, tvalSpringN800.120$p.value)
  , c (tvalSpringUNReg30$p.value, tvalSpringUNReg45$p.value, tvalSpringUNReg90$p.value, tvalSpringUNReg120$p.value)
  
  # 2011 only, MENA sample
  , c (tvalMENA11Cont30$p.value, tvalMENA11Cont45$p.value, tvalMENA11Cont90$p.value, tvalMENA11Cont120$p.value)
  , c (tvalMENA11N800.30$p.value, tvalMENA11N800.45$p.value, tvalMENA11N800.90$p.value, tvalMENA11N800.120$p.value)
  , rep (NA, 4)
)<0.05 ## NONE of the differences is statistically significant

odd <- seq (1, nrow (t11.all), by=2); even <- seq (2, nrow (t11.all), by=2)
row.names (t11.all2) <- as.numeric (c (odd, even))
t11.all2 <- as.matrix (t11.all2[order (as.numeric (row.names (t11.all2))),])
(t11.all3 <- cbind (
  t11.all2[,1:2]
  , rep ("", nrow(t11.all))
  , t11.all2[,3:4]
  , rep ("", nrow(t11.all))
  , t11.all2[,5:6]))
t11.all3[17:24,7:8] <- ""

rows <- rep (c ("\\textbf{30 days}", "", "\\textbf{45 days}", "", "\\textbf{90 days}", "", "\\textbf{120 days}", ""), 3)

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\textbf{$50$km}} & & \\multicolumn{2}{c}{\\textbf{$800$km}} & & \\multicolumn{2}{c}{\\textbf{UN region}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} \\\\ \\cmidrule{2-3} \\cmidrule{5-6} \\cmidrule{8-9} \n")
Header3 <- paste ("\\multicolumn{1}{l}{} & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} \\\\ \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{protest in last...}} & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} \\\\ \n")
Header5 <- paste ("\\midrule \\multicolumn{9}{c}{\\textbf{A. From original data}} \\\\ \\midrule \n")
Header6 <- paste ("\\midrule \\multicolumn{9}{c}{\\textbf{B. Adding protests in Tunisia, Libya and Syria}} \\\\ \\midrule \n")
Header7 <- paste ("\\midrule \\multicolumn{9}{c}{\\textbf{C. MENA countries only}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 0
addtorow$pos[[6]] <- 8
addtorow$pos[[7]] <- 16
addtorow$command <- c (Header1, Header2, Header3, Header4
                       , Header5, Header6, Header7)
print (xtable (cbind (rows, t11.all3)
               , align=c("c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Data Overview: 2011"
               , label="T:overview2011")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.55) Table A55: 2011 (1): Full Sample + Arab Spring Protests (Tunisia, Libya, Syria)

scaling <- 18*2

coefRob1503a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob15d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob15d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob15d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob15d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob15d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob15d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob15d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob15d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
  , unlist (lapply (mRob15d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,1]))
)
pvalRob1503a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob15d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob15d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob15d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[1,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob15d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob15d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob15d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[1,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob15d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob15d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
  , unlist (lapply (mRob15d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[1,4]))
)

# retrieving the estimates
coefRob1503 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1503a), 3)), ncol=9))
pvalRob1503 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1503a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1503a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1503 <- rbind (coefRob1503, pvalRob1503)[c (1, 1+nrow (coefRob1503)) + sort (rep ((0:(nrow (coefRob1503)-1)), 2)),]
(tabRob1503 <- cbind (tabRob1503[,1:3]
                      , rep ("", nrow (tabRob1503))
                      , tabRob1503[,4:6]
                      , rep ("", nrow (tabRob1503))
                      , tabRob1503[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nbspringCont01main[1]), " countries; ", sprintf ("%.0f", nbspringCont01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nbspringCont01main[3]/1000000, 2)), "m. obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nbspringN80001main[1]), " countries; ", sprintf ("%.0f", nbspringN80001main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nbspringN80001main[3]/1000000, 2)), "m. obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors} (", sprintf ("%.0f", nbspringUNReg01main[1]), " countries; ", sprintf ("%.0f", nbspringUNReg01main[2]), " country-years; $\\approx$ ", sprintf ("%.2f", round (nbspringUNReg01main[3]/1000000, 2)), "m. obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1503)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf 2011 (1): Full Sample $+$ Arab Spring Protests (Tunisia, Libya, Syria)"
               , label="T:robust201101")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.56) Table A56: 2011 (2): Full Sample + Arab Spring Protests (Tunisia, Libya, Syria) - 2011 Only

scaling <- 18*3

coefRob1504a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob15d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob15d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob15d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1504a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob15d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob15d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob15d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1504 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1504a), 3)), ncol=9))
pvalRob1504 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1504a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1504a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1504 <- rbind (coefRob1504, pvalRob1504)[c (1, 1+nrow (coefRob1504)) + sort (rep ((0:(nrow (coefRob1504)-1)), 2)),]
(tabRob1504 <- cbind (tabRob1504[,1:3]
                      , rep ("", nrow (tabRob1504))
                      , tabRob1504[,4:6]
                      , rep ("", nrow (tabRob1504))
                      , tabRob1504[,7:9]))

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nbspring11Cont01main[1]), " countries; ", sprintf ("%.0f", nbspring11Cont01main[3]), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nbspring11N80001main[1]), " countries; ", sprintf ("%.0f", nbspring11N80001main[3]), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. MENA Region Neighbors} (", sprintf ("%.0f", nbspring11UNReg01main[1]), " countries; ", sprintf ("%.0f", nbspring11UNReg01main[3]), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1504)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf 2011 (2): Full Sample $+$ Arab Spring Protests (Tunisia, Libya, Syria) -- 2011 only"
               , label="T:robust201102")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.57) Table A57: 2011 (3): MENA Sample - 2011 Only

scaling <- 18*8

coefRob1509a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob15d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,1]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob15d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,1]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob15d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
  , unlist (lapply (mRob15d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,1]))
)
pvalRob1509a <- cbind (
  
  # at least one neighbor protest
  unlist (lapply (mRob15d45[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d90[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d120[c(1, 10, 4, 13, 7, 16)+scaling], FUN=function (x) x[2,4]))
  
  # total number of neighbor protests
  , unlist (lapply (mRob15d45[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d90[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d120[c(2, 11, 5, 14, 8, 17)+scaling], FUN=function (x) x[2,4]))
  
  # size of largest neighbor protest
  , unlist (lapply (mRob15d45[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d90[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
  , unlist (lapply (mRob15d120[c(3, 12, 6, 15, 9, 18)+scaling], FUN=function (x) x[2,4]))
)

# retrieving the estimates
coefRob1509 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob1509a), 3)), ncol=9))
pvalRob1509 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob1509a, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob1509a <= 0.05) == 0, "", "*")
  , sep="")
  , nrow=6, ncol=9)
tabRob1509 <- rbind (coefRob1509, pvalRob1509)[c (1, 1+nrow (coefRob1509)) + sort (rep ((0:(nrow (coefRob1509)-1)), 2)),]
(tabRob1509 <- cbind (tabRob1509[,1:3]
                      , rep ("", nrow (tabRob1509))
                      , tabRob1509[,4:6]
                      , rep ("", nrow (tabRob1509))
                      , tabRob1509[,7:9]))
tabRob1509[9:12,1:3] <- ""

rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{At Least One Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Total Number of Neighbor}} & & \\multicolumn{3}{c}{\\textbf{Size of Largest Neighbor}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\textbf{Protest in Last}} & & \\multicolumn{3}{c}{\\textbf{Protests in Last}} & & \\multicolumn{3}{c}{\\textbf{Protest in Last}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \\cmidrule{10-12} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{A. 50km Neighbors} (", sprintf ("%.0f", nbmena11Cont01main[1]), " countries; ", sprintf ("%.0f", nbmena11Cont01main[3]), " obs.)} \\\\ \\midrule \n", sep="")
Header5 <- paste ("[1.5ex] \\multicolumn{12}{l}{\\textbf{\\emph{Baseline $+$ Election}}} \\\\ \\midrule \n")
Header6 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{B. 800km Neighbors} (", sprintf ("%.0f", nbmena11N80001main[1]), " countries; ", sprintf ("%.0f", nbmena11N80001main[3]), " obs.)} \\\\ \\midrule \n", sep="")
Header7 <- paste ("\\toprule \\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & & \\multicolumn{10}{c}{\\textbf{C. UN Region Neighbors$^{\\dagger}$} (", sprintf ("%.0f", nbmena11UNReg01main[1]), " countries; ", sprintf ("%.0f", nbmena11UNReg01main[3]), " obs.)} \\\\ \\midrule \n", sep="")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 6
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 10
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5
                       , Header6, Header5, Header7, Header5)
print (xtable (cbind (rows, tabRob1509)
               , align=c("l","l","c","c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf 2011 (3): MENA Sample -- 2011 Only"
               , label="T:robust201103")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.58) Table A58: Democracy Protests, 1989-2011: Conditional Logit Specifications

# getting the coefficients
coefRob06 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mContRob06[1:3], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob06[7:9], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob06[13:15], FUN=function (x) x[1,1])) ## interaction
  , unlist (lapply (mContRob06[13:15], FUN=function (x) x[2,1]))
  , unlist (lapply (mContRob06[13:15], FUN=function (x) x[3,1]))
)

# 800-km neighbors: baseline
, rbind (
  unlist (lapply (mN800Rob06[1:3], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob06[7:9], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob06[13:15], FUN=function (x) x[1,1])) ## interaction
  , unlist (lapply (mN800Rob06[13:15], FUN=function (x) x[2,1]))
  , unlist (lapply (mN800Rob06[13:15], FUN=function (x) x[3,1]))
))
pvalRob06 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mContRob06[1:3], FUN=function (x) x[1,5]))
  , unlist (lapply (mContRob06[7:9], FUN=function (x) x[1,5]))
  , unlist (lapply (mContRob06[13:15], FUN=function (x) x[1,5])) ## interaction
  , unlist (lapply (mContRob06[13:15], FUN=function (x) x[2,5]))
  , unlist (lapply (mContRob06[13:15], FUN=function (x) x[3,5]))
)

# 800-km neighbors: baseline
, rbind (
  unlist (lapply (mN800Rob06[1:3], FUN=function (x) x[1,5]))
  , unlist (lapply (mN800Rob06[7:9], FUN=function (x) x[1,5]))
  , unlist (lapply (mN800Rob06[13:15], FUN=function (x) x[1,5])) ## interaction
  , unlist (lapply (mN800Rob06[13:15], FUN=function (x) x[2,5]))
  , unlist (lapply (mN800Rob06[13:15], FUN=function (x) x[3,5]))
))

# retrieving the estimates
coefRob06 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob06), 3)), ncol=6))
pvalRob06 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob06, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob06<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob06), ncol=6)
tabRob06 <- rbind (coefRob06, pvalRob06)[c (1, 1+nrow (coefRob06)) + sort (rep ((0:(nrow (coefRob06)-1)), 2)),]
tabRob06 <- cbind (tabRob06[,1:3]
                   , rep ("", nrow (tabRob06))
                   , tabRob06[,4:6])

# building the table
rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "~~~~~$\\times$ \\emph{Election}"
           , "\\emph{Election}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsContRob06[1]), " c's; ", sprintf ("%.0f", nobsContRob06[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsContRob06[3]/1000000, 2)), "m obs.)} & & \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsN800Rob06[1]), " c's; ", sprintf ("%.0f", nobsN800Rob06[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsN800Rob06[3]/1000000, 2)), "m obs.)} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n", sep="")
Header3 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline $+$ Election Control}}} \\\\ \\midrule \n")
Header5 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Election Interaction}}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 2
addtorow$pos[[5]] <- 4
addtorow$command <- c (Header1, Header2, Header3
                       , Header4, Header5)
print (xtable (cbind (rows, tabRob06)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Democracy Protests, 1989-2011: Conditional Logit Specifications"
               , label="T:resultsCLogit")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.59) Table A59: Data Overview: Post-election Periods Only
tab.ele <- rbind (
  cbind (rbind (tabCont.ele45[2,], tabCont.ele90[2,], tabCont.ele120[2,])
         , rbind (tabN800.ele45[2,], tabN800.ele90[2,], tabN800.ele120[2,])
         , rbind (tabUNReg.ele45[2,], tabUNReg.ele90[2,], tabUNReg.ele120[2,]))
  , cbind (rbind (tabCont.ele45abs[2,], tabCont.ele90abs[2,], tabCont.ele120abs[2,])
           , rbind (tabN800.ele45abs[2,], tabN800.ele90abs[2,], tabN800.ele120abs[2,])
           , rbind (tabUNReg.ele45abs[2,], tabUNReg.ele90abs[2,], tabUNReg.ele120abs[2,])))
tab.ele2 <- as.data.frame (rbind (
  matrix (paste (sprintf ("%.3f", round (tab.ele[1:3,], 3)), "\\%", sep=""), ncol=6)
  , matrix (paste ("[", sprintf ("%.0f", round (tab.ele[4:6,], 0)), "]", sep=""), ncol=6)))

# adding the asterisks to indicate statistically significant differences
tab.ele2$V2 <- as.character (tab.ele2$V2)
tab.ele2$V4 <- as.character (tab.ele2$V4)
tab.ele2$V6 <- as.character (tab.ele2$V6)
cbind (c (tvalCont.ele45$p.value, tvalCont.ele90$p.value, tvalCont.ele120$p.value)
       , c (tvalN800.ele45$p.value, tvalN800.ele90$p.value, tvalN800.ele120$p.value)
       , c (tvalUNReg.ele45$p.value, tvalUNReg.ele90$p.value, tvalUNReg.ele120$p.value))<0.05 ## no cases

odd <- seq (1, nrow (tab.ele), by=2); even <- seq (2, nrow (tab.ele), by=2)
row.names (tab.ele2) <- as.numeric (c (odd, even))
tab.ele2 <- as.matrix (tab.ele2[order (as.numeric (row.names (tab.ele2))),])
(tab.ele3 <- cbind (
  tab.ele2[,1:2]
  , rep ("", nrow(tab.ele))
  , tab.ele2[,3:4]
  , rep ("", nrow(tab.ele))
  , tab.ele2[,5:6]))

rows <- c ("\\textbf{45 days}", "", "\\textbf{90 days}", "", "\\textbf{120 days}", "")

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\textbf{$50$km}} & & \\multicolumn{2}{c}{\\textbf{$800$km}} & & \\multicolumn{2}{c}{\\textbf{UN region}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} \\\\ \\cmidrule{2-3} \\cmidrule{5-6} \\cmidrule{8-9} \n")
Header3 <- paste ("\\multicolumn{1}{l}{} & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} \\\\ \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{protest in last...}} & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$command <- c (Header1, Header2, Header3, Header4)
print (xtable (cbind (rows, tab.ele3)
               , align=c("c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Data Overview: Post-election Periods Only"
               , label="T:overviewPostElection")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.60) Table A60: Data Overview: Non-election Periods Only
tab.noele <- rbind (
  cbind (rbind (tabCont.noele45[2,], tabCont.noele90[2,], tabCont.noele120[2,])
         , rbind (tabN800.noele45[2,], tabN800.noele90[2,], tabN800.noele120[2,])
         , rbind (tabUNReg.noele45[2,], tabUNReg.noele90[2,], tabUNReg.noele120[2,]))
  , cbind (rbind (tabCont.noele45abs[2,], tabCont.noele90abs[2,], tabCont.noele120abs[2,])
           , rbind (tabN800.noele45abs[2,], tabN800.noele90abs[2,], tabN800.noele120abs[2,])
           , rbind (tabUNReg.noele45abs[2,], tabUNReg.noele90abs[2,], tabUNReg.noele120abs[2,])))
tab.noele2 <- as.data.frame (rbind (
  matrix (paste (sprintf ("%.3f", round (tab.noele[1:3,], 3)), "\\%", sep=""), ncol=6)
  , matrix (paste ("[", sprintf ("%.0f", round (tab.noele[4:6,], 0)), "]", sep=""), ncol=6)))

# adding the asterisks to indicate statistically significant differences
tab.noele2$V2 <- as.character (tab.noele2$V2)
tab.noele2$V4 <- as.character (tab.noele2$V4)
tab.noele2$V6 <- as.character (tab.noele2$V6)
cbind (c (tvalCont.noele45$p.value, tvalCont.noele90$p.value, tvalCont.noele120$p.value)
       , c (tvalN800.noele45$p.value, tvalN800.noele90$p.value, tvalN800.noele120$p.value)
       , c (tvalUNReg.noele45$p.value, tvalUNReg.noele90$p.value, tvalUNReg.noele120$p.value))<0.05 ## only one case
tab.noele2[2,4] <- paste (tab.noele2[2,4], "*", sep="")

odd <- seq (1, nrow (tab.noele), by=2); even <- seq (2, nrow (tab.noele), by=2)
row.names (tab.noele2) <- as.numeric (c (odd, even))
tab.noele2 <- as.matrix (tab.noele2[order (as.numeric (row.names (tab.noele2))),])
(tab.noele3 <- cbind (
  tab.noele2[,1:2]
  , rep ("", nrow(tab.noele))
  , tab.noele2[,3:4]
  , rep ("", nrow(tab.noele))
  , tab.noele2[,5:6]))

rows <- c ("\\textbf{45 days}", "", "\\textbf{90 days}", "", "\\textbf{120 days}", "")

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\textbf{$50$km}} & & \\multicolumn{2}{c}{\\textbf{$800$km}} & & \\multicolumn{2}{c}{\\textbf{UN region}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} & & \\multicolumn{2}{c}{\\textbf{neighbors}} \\\\ \\cmidrule{2-3} \\cmidrule{5-6} \\cmidrule{8-9} \n")
Header3 <- paste ("\\multicolumn{1}{l}{} & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} & & \\multicolumn{1}{c}{\\textbf{no neighbor}} & \\multicolumn{1}{c}{\\textbf{neighbor}} \\\\ \n")
Header4 <- paste ("\\multicolumn{1}{l}{\\textbf{protest in last...}} & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} & & \\multicolumn{1}{c}{\\textbf{protest}} & \\multicolumn{1}{c}{\\textbf{protest}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$command <- c (Header1, Header2, Header3, Header4)
print (xtable (cbind (rows, tab.noele3)
               , align=c("c","c","c","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Data Overview: Non-election Periods Only"
               , label="T:overviewNonElection")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.61) Table A61: Interaction between Protests and Elections, 1989-2011

# getting the coefficients
coefElection01 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mContRob01[55:57], FUN=function (x) x[1,1])) ## dummy
  , unlist (lapply (mContRob01[55:57], FUN=function (x) x[3,1]))
  , unlist (lapply (mContRob01[55:57], FUN=function (x) x[2,1]))
  , unlist (lapply (mContRob01[58:60], FUN=function (x) x[1,1])) ## sum
  , unlist (lapply (mContRob01[58:60], FUN=function (x) x[3,1]))
  , unlist (lapply (mContRob01[58:60], FUN=function (x) x[2,1]))
  , unlist (lapply (mContRob01[61:63], FUN=function (x) x[1,1])) ## size
  , unlist (lapply (mContRob01[61:63], FUN=function (x) x[3,1]))
  , unlist (lapply (mContRob01[61:63], FUN=function (x) x[2,1]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[1,1])) ## success
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[2,1]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[3,1]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[5,1])) ## success (int)
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[6,1]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[7,1]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[4,1])) ## (election only)
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[1,1])) ## repression
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[2,1]))
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[3,1]))
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[5,1])) ## repression (int)
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[6,1]))
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[7,1]))
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[4,1])) ## (election only)
)

# 800-km neighbors: baseline
, rbind (
  unlist (lapply (mN800Rob01[55:57], FUN=function (x) x[1,1])) ## dummy
  , unlist (lapply (mN800Rob01[55:57], FUN=function (x) x[3,1]))
  , unlist (lapply (mN800Rob01[55:57], FUN=function (x) x[2,1]))
  , unlist (lapply (mN800Rob01[58:60], FUN=function (x) x[1,1])) ## sum
  , unlist (lapply (mN800Rob01[58:60], FUN=function (x) x[3,1]))
  , unlist (lapply (mN800Rob01[58:60], FUN=function (x) x[2,1]))
  , unlist (lapply (mN800Rob01[61:63], FUN=function (x) x[1,1])) ## size
  , unlist (lapply (mN800Rob01[61:63], FUN=function (x) x[3,1]))
  , unlist (lapply (mN800Rob01[61:63], FUN=function (x) x[2,1]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[1,1])) ## success
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[2,1]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[3,1]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[5,1])) ## success (int)
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[6,1]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[7,1]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[4,1])) ## (election only)
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[1,1])) ## repression
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[2,1]))
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[3,1]))
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[5,1])) ## repression (int)
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[6,1]))
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[7,1]))
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[4,1])) ## (election only)
))
pvalElection01 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mContRob01[55:57], FUN=function (x) x[1,4])) ## dummy
  , unlist (lapply (mContRob01[55:57], FUN=function (x) x[3,4]))
  , unlist (lapply (mContRob01[55:57], FUN=function (x) x[2,4]))
  , unlist (lapply (mContRob01[58:60], FUN=function (x) x[1,4])) ## sum
  , unlist (lapply (mContRob01[58:60], FUN=function (x) x[3,4]))
  , unlist (lapply (mContRob01[58:60], FUN=function (x) x[2,4]))
  , unlist (lapply (mContRob01[61:63], FUN=function (x) x[1,4])) ## size
  , unlist (lapply (mContRob01[61:63], FUN=function (x) x[3,4]))
  , unlist (lapply (mContRob01[61:63], FUN=function (x) x[2,4]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[1,4])) ## success
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[2,4]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[3,4]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[5,4])) ## success (int)
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[6,4]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[7,4]))
  , unlist (lapply (mContRob01[64:66], FUN=function (x) x[4,4])) ## (election only)
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[1,4])) ## repression
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[2,4]))
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[3,4]))
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[5,4])) ## repression (int)
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[6,4]))
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[7,4]))
  , unlist (lapply (mContRob01[67:69], FUN=function (x) x[4,4])) ## (election only)
)

# 800-km neighbors: baseline
, rbind (
  unlist (lapply (mN800Rob01[55:57], FUN=function (x) x[1,4])) ## dummy
  , unlist (lapply (mN800Rob01[55:57], FUN=function (x) x[3,4]))
  , unlist (lapply (mN800Rob01[55:57], FUN=function (x) x[2,4]))
  , unlist (lapply (mN800Rob01[58:60], FUN=function (x) x[1,4])) ## sum
  , unlist (lapply (mN800Rob01[58:60], FUN=function (x) x[3,4]))
  , unlist (lapply (mN800Rob01[58:60], FUN=function (x) x[2,4]))
  , unlist (lapply (mN800Rob01[61:63], FUN=function (x) x[1,4])) ## size
  , unlist (lapply (mN800Rob01[61:63], FUN=function (x) x[3,4]))
  , unlist (lapply (mN800Rob01[61:63], FUN=function (x) x[2,4]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[1,4])) ## success
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[2,4]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[3,4]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[5,4])) ## success (int)
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[6,4]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[7,4]))
  , unlist (lapply (mN800Rob01[64:66], FUN=function (x) x[4,4])) ## (election only)
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[1,4])) ## repression
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[2,4]))
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[3,4]))
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[5,4])) ## repression (int)
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[6,4]))
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[7,4]))
  , unlist (lapply (mN800Rob01[67:69], FUN=function (x) x[4,4])) ## (election only)
))

# retrieving the estimates
coefElection01 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefElection01), 3)), ncol=6))
coefElection01[grep ("NaN", coefElection01)] <- ""
pvalElection01 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalElection01, 3)), ")", sep="")
  , ifelse (as.numeric (pvalElection01<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefElection01), ncol=6)
pvalElection01[grep ("NaN", pvalElection01)] <- ""
tabElection01 <- rbind (coefElection01, pvalElection01)[c (1, 1+nrow (coefElection01)) + sort (rep ((0:(nrow (coefElection01)-1)), 2)),]
tabElection01 <- cbind (tabElection01[,1:3]
                        , rep ("", nrow (tabElection01))
                        , tabElection01[,4:6])

# building the table
rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "~~~~~$\\times$ \\emph{Election}"
           , "\\emph{Election}", ""
           , "\\emph{Neighbor Democracy Protests}", "~~~~~(sum)"
           , "\\emph{Neighbor Democracy Protests}", "~~~~~(sum) $\\times$ \\emph{Election}"
           , "\\emph{Election}", ""
           , "\\emph{Neighbor Democracy Protest}", "~~~~~(size)"
           , "\\emph{Neighbor Democracy Protest}", "~~~~~(size) $\\times$ \\emph{Election}"
           , "\\emph{Election}", ""
           , "\\emph{All Successful}", ""
           , "\\emph{Some Successful}", ""
           , "\\emph{None Successful}", ""
           , "\\emph{All Successful}", "~~~~~$\\times$ \\emph{Election}"
           , "\\emph{Some Successful}", "~~~~~$\\times$ \\emph{Election}"
           , "\\emph{None Successful}", "~~~~~$\\times$ \\emph{Election}"
           , "\\emph{Election}", ""
           , "\\emph{All Repressed}", ""
           , "\\emph{Some Repressed}", ""
           , "\\emph{None Repressed}", ""
           , "\\emph{All Repressed}", "~~~~~$\\times$ \\emph{Election}"
           , "\\emph{Some Repressed}", "~~~~~$\\times$ \\emph{Election}"
           , "\\emph{None Repressed}", "~~~~~$\\times$ \\emph{Election}"
           , "\\emph{Election}", "")                

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsContRob01.high[1]), " c's; ", sprintf ("%.0f", nobsContRob01.high[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsContRob01.high[3]/1000000, 2)), "m obs.)} & & \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsN800Rob01.high[1]), " c's; ", sprintf ("%.0f", nobsN800Rob01.high[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsN800Rob01.high[3]/1000000, 2)), "m obs.)} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n", sep="")
Header3 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("[0.75ex] \\multicolumn{8}{l}{\\textbf{\\emph{Sum of neighbor protests}}} \\\\ \\midrule \n")
Header5 <- paste ("[0.75ex] \\multicolumn{8}{l}{\\textbf{\\emph{Neighbor rotest size}}} \\\\ \\midrule \n")
Header6 <- paste ("[0.75ex] \\multicolumn{8}{l}{\\textbf{\\emph{Neighbor protest success}}} \\\\ \\midrule \n")
Header7 <- paste ("[0.75ex] \\multicolumn{8}{l}{\\textbf{\\emph{Neighbor protest repression}}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 6
addtorow$pos[[5]] <- 12
addtorow$pos[[6]] <- 18
addtorow$pos[[7]] <- 32
addtorow$command <- c (Header1, Header2, Header3
                       , Header4, Header5, Header6, Header7)
print (xtable (cbind (rows, tabElection01)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Interaction between Protests and Elections, 1989-2011"
               , label="T:resultsElections01")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="scriptsize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.62) Table A62: Interaction between Protests and Elections, 1989-2011: Election-related Protests (in Neighbor) Only

# getting the coefficients
coefElection02 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mContRob05[1:3], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob05[4:6], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob05[7:9], FUN=function (x) x[1,1])) ## interaction
  , unlist (lapply (mContRob05[7:9], FUN=function (x) x[2,1]))
  , unlist (lapply (mContRob05[7:9], FUN=function (x) x[3,1]))
)

# 800-km neighbors: baseline
, rbind (
  unlist (lapply (mN800Rob05[1:3], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob05[4:6], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob05[7:9], FUN=function (x) x[1,1])) ## interaction
  , unlist (lapply (mN800Rob05[7:9], FUN=function (x) x[2,1]))
  , unlist (lapply (mN800Rob05[7:9], FUN=function (x) x[3,1]))
))
pvalElection02 <- cbind (rbind (
  
  # contiguous neighbors: baseline
  unlist (lapply (mContRob05[1:3], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob05[4:6], FUN=function (x) x[1,4]))
  , unlist (lapply (mContRob05[7:9], FUN=function (x) x[1,4])) ## interaction
  , unlist (lapply (mContRob05[7:9], FUN=function (x) x[2,4]))
  , unlist (lapply (mContRob05[7:9], FUN=function (x) x[3,4]))
)

# 800-km neighbors: baseline
, rbind (
  unlist (lapply (mN800Rob05[1:3], FUN=function (x) x[1,4]))
  , unlist (lapply (mN800Rob05[4:6], FUN=function (x) x[1,4]))
  , unlist (lapply (mN800Rob05[7:9], FUN=function (x) x[1,4])) ## interaction
  , unlist (lapply (mN800Rob05[7:9], FUN=function (x) x[2,4]))
  , unlist (lapply (mN800Rob05[7:9], FUN=function (x) x[3,4]))
))

# retrieving the estimates
coefElection02 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefElection02), 3)), ncol=6))
pvalElection02 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalElection02, 3)), ")", sep="")
  , ifelse (as.numeric (pvalElection02<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefElection02), ncol=6)
tabElection02 <- rbind (coefElection02, pvalElection02)[c (1, 1+nrow (coefElection02)) + sort (rep ((0:(nrow (coefElection02)-1)), 2)),]
tabElection02 <- cbind (tabElection02[,1:3]
                        , rep ("", nrow (tabElection02))
                        , tabElection02[,4:6])

# building the table
rows <- c ("\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", ""
           , "\\emph{Neighbor Democracy Protest}", "~~~~~$\\times$ \\emph{Election}"
           , "\\emph{Election}", "")

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsContRob04[1]), " c's; ", sprintf ("%.0f", nobsContRob04[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsContRob04[3]/1000000, 2)), "m obs.)} & & \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsN800Rob04[1]), " c's; ", sprintf ("%.0f", nobsN800Rob04[2]), " c-years; $\\approx$ ", sprintf ("%.2f", round (nobsN800Rob04[3]/1000000, 2)), "m obs.)} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n", sep="")
Header3 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{Baseline}}} & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header4 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Baseline $+$ Election Control}}} \\\\ \\midrule \n")
Header5 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{Election Interaction}}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 2
addtorow$pos[[5]] <- 4
addtorow$command <- c (Header1, Header2, Header3
                       , Header4, Header5)
print (xtable (cbind (rows, tabElection02)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Interaction between Protests and Elections, 1989-2011: Election-related Protests (in Neighbor) Only"
               , label="T:resultsElections02")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.63) Table A63: Election-related Protest in Last Election in Neighbor, 1989-2011

coefRob07 <- cbind (
  
  ## 50km, dummy
  
  # logit
  c (unlist (lapply (mContRob07[16], FUN=function (x) x[2,1])) ## within 30 days
     , unlist (lapply (mContRob07[19], FUN=function (x) x[2,1])) ## all successful
     , unlist (lapply (mContRob07[19], FUN=function (x) x[3,1])) ## some successful
     , unlist (lapply (mContRob07[19], FUN=function (x) x[4,1])) ## none successful
     , unlist (lapply (mContRob07[20], FUN=function (x) x[2,1])) ## all repressed
     , unlist (lapply (mContRob07[20], FUN=function (x) x[3,1])) ## some repressed
     , unlist (lapply (mContRob07[20], FUN=function (x) x[4,1])) ## none repressed
     
     # LPM's
     , unlist (lapply (mContRob07[1], FUN=function (x) x[1,1]))  ## within 30 days
     , unlist (lapply (mContRob07[4], FUN=function (x) x[1,1]))  ## all successful
     , unlist (lapply (mContRob07[4], FUN=function (x) x[2,1]))  ## some successful
     , unlist (lapply (mContRob07[4], FUN=function (x) x[3,1]))  ## none successful
     , unlist (lapply (mContRob07[5], FUN=function (x) x[1,1]))  ## all repressed
     , unlist (lapply (mContRob07[5], FUN=function (x) x[2,1]))  ## some repressed
     , unlist (lapply (mContRob07[5], FUN=function (x) x[3,1]))  ## none repressed
  )
  
  ## 50km, sum + size
  , c (unlist (lapply (mContRob07[17], FUN=function (x) x[2,1]))
       , rep (NA, 6)
       , unlist (lapply (mContRob07[2], FUN=function (x) x[1,1]))
       , rep (NA, 6))
  , c (unlist (lapply (mContRob07[18], FUN=function (x) x[2,1]))
       , rep (NA, 6)
       , unlist (lapply (mContRob07[3], FUN=function (x) x[1,1]))
       , rep (NA, 6))
  
  
  ## 800km, dummy
  
  # logit
  , c (unlist (lapply (mN800Rob07[16], FUN=function (x) x[2,1])) ## within 30 days
       , unlist (lapply (mN800Rob07[19], FUN=function (x) x[2,1])) ## all successful
       , unlist (lapply (mN800Rob07[19], FUN=function (x) x[3,1])) ## some successful
       , unlist (lapply (mN800Rob07[19], FUN=function (x) x[4,1])) ## none successful
       , unlist (lapply (mN800Rob07[20], FUN=function (x) x[2,1])) ## all repressed
       , unlist (lapply (mN800Rob07[20], FUN=function (x) x[3,1])) ## some repressed
       , unlist (lapply (mN800Rob07[20], FUN=function (x) x[4,1])) ## none repressed
       
       # LPM's
       , unlist (lapply (mN800Rob07[1], FUN=function (x) x[1,1]))  ## within 30 days
       , unlist (lapply (mN800Rob07[4], FUN=function (x) x[1,1]))  ## all successful
       , unlist (lapply (mN800Rob07[4], FUN=function (x) x[2,1]))  ## some successful
       , unlist (lapply (mN800Rob07[4], FUN=function (x) x[3,1]))  ## none successful
       , unlist (lapply (mN800Rob07[5], FUN=function (x) x[1,1]))  ## all repressed
       , unlist (lapply (mN800Rob07[5], FUN=function (x) x[2,1]))  ## some repressed
       , unlist (lapply (mN800Rob07[5], FUN=function (x) x[3,1]))  ## none repressed
  )
  
  ## 800km, sum + size
  , c (unlist (lapply (mN800Rob07[17], FUN=function (x) x[2,1]))
       , rep (NA, 6)
       , unlist (lapply (mN800Rob07[2], FUN=function (x) x[1,1]))
       , rep (NA, 6))
  , c (unlist (lapply (mN800Rob07[18], FUN=function (x) x[2,1]))
       , rep (NA, 6)
       , unlist (lapply (mN800Rob07[3], FUN=function (x) x[1,1]))
       , rep (NA, 6))
)
pvalRob07 <- cbind (
  
  ## 50km, dummy
  
  # logit
  c (unlist (lapply (mContRob07[16], FUN=function (x) x[2,4])) ## within 30 days
     , unlist (lapply (mContRob07[19], FUN=function (x) x[2,4])) ## all successful
     , unlist (lapply (mContRob07[19], FUN=function (x) x[3,4])) ## some successful
     , unlist (lapply (mContRob07[19], FUN=function (x) x[4,4])) ## none successful
     , unlist (lapply (mContRob07[20], FUN=function (x) x[2,4])) ## all repressed
     , unlist (lapply (mContRob07[20], FUN=function (x) x[3,4])) ## some repressed
     , unlist (lapply (mContRob07[20], FUN=function (x) x[4,4])) ## none repressed
     
     # LPM's
     , unlist (lapply (mContRob07[1], FUN=function (x) x[1,4]))  ## within 30 days
     , unlist (lapply (mContRob07[4], FUN=function (x) x[1,4]))  ## all successful
     , unlist (lapply (mContRob07[4], FUN=function (x) x[2,4]))  ## some successful
     , unlist (lapply (mContRob07[4], FUN=function (x) x[3,4]))  ## none successful
     , unlist (lapply (mContRob07[5], FUN=function (x) x[1,4]))  ## all repressed
     , unlist (lapply (mContRob07[5], FUN=function (x) x[2,4]))  ## some repressed
     , unlist (lapply (mContRob07[5], FUN=function (x) x[3,4]))  ## none repressed
  )
  
  ## 50km, sum + size
  , c (unlist (lapply (mContRob07[17], FUN=function (x) x[2,4]))
       , rep (NA, 6)
       , unlist (lapply (mContRob07[2], FUN=function (x) x[1,4]))
       , rep (NA, 6))
  , c (unlist (lapply (mContRob07[18], FUN=function (x) x[2,4]))
       , rep (NA, 6)
       , unlist (lapply (mContRob07[3], FUN=function (x) x[1,4]))
       , rep (NA, 6))
  
  
  ## 800km, dummy
  
  # logit
  , c (unlist (lapply (mN800Rob07[16], FUN=function (x) x[2,4])) ## within 30 days
       , unlist (lapply (mN800Rob07[19], FUN=function (x) x[2,4])) ## all successful
       , unlist (lapply (mN800Rob07[19], FUN=function (x) x[3,4])) ## some successful
       , unlist (lapply (mN800Rob07[19], FUN=function (x) x[4,4])) ## none successful
       , unlist (lapply (mN800Rob07[20], FUN=function (x) x[2,4])) ## all repressed
       , unlist (lapply (mN800Rob07[20], FUN=function (x) x[3,4])) ## some repressed
       , unlist (lapply (mN800Rob07[20], FUN=function (x) x[4,4])) ## none repressed
       
       # LPM's
       , unlist (lapply (mN800Rob07[1], FUN=function (x) x[1,4]))  ## within 30 days
       , unlist (lapply (mN800Rob07[4], FUN=function (x) x[1,4]))  ## all successful
       , unlist (lapply (mN800Rob07[4], FUN=function (x) x[2,4]))  ## some successful
       , unlist (lapply (mN800Rob07[4], FUN=function (x) x[3,4]))  ## none successful
       , unlist (lapply (mN800Rob07[5], FUN=function (x) x[1,4]))  ## all repressed
       , unlist (lapply (mN800Rob07[5], FUN=function (x) x[2,4]))  ## some repressed
       , unlist (lapply (mN800Rob07[5], FUN=function (x) x[3,4]))  ## none repressed
  )
  
  ## 800km, sum + size
  , c (unlist (lapply (mN800Rob07[17], FUN=function (x) x[2,4]))
       , rep (NA, 6)
       , unlist (lapply (mN800Rob07[2], FUN=function (x) x[1,4]))
       , rep (NA, 6))
  , c (unlist (lapply (mN800Rob07[18], FUN=function (x) x[2,4]))
       , rep (NA, 6)
       , unlist (lapply (mN800Rob07[3], FUN=function (x) x[1,4]))
       , rep (NA, 6))
)

## retrieving the estimates
coefRob07 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob07), 3)), ncol=6))
coefRob07[coefRob07=="NA"] <- ""  ## to get rid of the NA's
pvalRob07 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob07, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob07<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob07), ncol=6)
pvalRob07[pvalRob07=="(NA)NA"] <- ""
tabRob07 <- rbind (coefRob07, pvalRob07)[c (1, 1+nrow (coefRob07)) + sort (rep ((0:(nrow (coefRob07)-1)), 2)),]
tabRob07 <- cbind (tabRob07[,1:3]
                   , rep ("", nrow (tabRob07))
                   , tabRob07[,4:6])

## building the table
rows <- rep (c ("\\emph{Neighbor Democracy Protest}", "~~~~~\\emph{(last election)}"
                , "\\emph{All Successful}", ""
                , "\\emph{Some Successful}", ""
                , "\\emph{None Successful}", ""
                , "\\emph{All Repressed}", ""
                , "\\emph{Some Repressed}", ""
                , "\\emph{None Repressed}", ""), 2)

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n")
Header2 <- paste ("\\multicolumn{1}{l}{\\textbf{(a) \\emph{Logit, country REs}}} & \\multicolumn{1}{c}{\\textbf{dummy}} & \\multicolumn{1}{c}{\\textbf{sum}} & \\multicolumn{1}{c}{\\textbf{size}} & & \\multicolumn{1}{c}{\\textbf{dummy}} & \\multicolumn{1}{c}{\\textbf{sum}} & \\multicolumn{1}{c}{\\textbf{size}} \\\\ \\midrule \n")
Header3 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{(b) \\emph{LPM, country REs}}} \\\\ \\midrule \n")
Header4 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\underline{Protest success}} \\\\ [1.5ex] \n")
Header5 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\underline{Government repression}} \\\\ [1.5ex] \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 2
addtorow$pos[[4]] <- 8
addtorow$pos[[5]] <- 14
addtorow$pos[[6]] <- 16
addtorow$pos[[7]] <- 22
addtorow$command <- c (Header1, Header2, Header4, Header5
                       , Header3, Header4, Header5)
print (xtable (cbind (rows, tabRob07)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Election-related Protest in Last Election in Neighbor, 1989-2011"
               , label="T:resultsElections03")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.64) Table A64: Election-related Protest in Last Election in Neighbor, 2000-2005 Only

coefRob07b <- cbind (
  
  ## 50km, dummy
  
  # logit
  c (unlist (lapply (mContRob07b[16], FUN=function (x) x[2,1])) ## within 30 days
     , unlist (lapply (mContRob07b[19], FUN=function (x) x[2,1])) ## all successful
     , unlist (lapply (mContRob07b[19], FUN=function (x) x[3,1])) ## some successful
     , unlist (lapply (mContRob07b[19], FUN=function (x) x[4,1])) ## none successful
     , unlist (lapply (mContRob07b[20], FUN=function (x) x[2,1])) ## all repressed
     , unlist (lapply (mContRob07b[20], FUN=function (x) x[3,1])) ## some repressed
     , unlist (lapply (mContRob07b[20], FUN=function (x) x[4,1])) ## none repressed
     
     # LPM's
     , unlist (lapply (mContRob07b[1], FUN=function (x) x[1,1]))  ## within 30 days
     , unlist (lapply (mContRob07b[4], FUN=function (x) x[1,1]))  ## all successful
     , unlist (lapply (mContRob07b[4], FUN=function (x) x[2,1]))  ## some successful
     , unlist (lapply (mContRob07b[4], FUN=function (x) x[3,1]))  ## none successful
     , unlist (lapply (mContRob07b[5], FUN=function (x) x[1,1]))  ## all repressed
     , unlist (lapply (mContRob07b[5], FUN=function (x) x[2,1]))  ## some repressed
     , unlist (lapply (mContRob07b[5], FUN=function (x) x[3,1]))  ## none repressed
  )
  
  ## 50km, sum + size
  , c (unlist (lapply (mContRob07b[17], FUN=function (x) x[2,1]))
       , rep (NA, 6)
       , unlist (lapply (mContRob07b[2], FUN=function (x) x[1,1]))
       , rep (NA, 6))
  , c (unlist (lapply (mContRob07b[18], FUN=function (x) x[2,1]))
       , rep (NA, 6)
       , unlist (lapply (mContRob07b[3], FUN=function (x) x[1,1]))
       , rep (NA, 6))
  
  
  ## 800km, dummy
  
  # logit
  , c (unlist (lapply (mN800Rob07b[16], FUN=function (x) x[2,1])) ## within 30 days
       , unlist (lapply (mN800Rob07b[19], FUN=function (x) x[2,1])) ## all successful
       , unlist (lapply (mN800Rob07b[19], FUN=function (x) x[3,1])) ## some successful
       , unlist (lapply (mN800Rob07b[19], FUN=function (x) x[4,1])) ## none successful
       , unlist (lapply (mN800Rob07b[20], FUN=function (x) x[2,1])) ## all repressed
       , unlist (lapply (mN800Rob07b[20], FUN=function (x) x[3,1])) ## some repressed
       , unlist (lapply (mN800Rob07b[20], FUN=function (x) x[4,1])) ## none repressed
       
       # LPM's
       , unlist (lapply (mN800Rob07b[1], FUN=function (x) x[1,1]))  ## within 30 days
       , unlist (lapply (mN800Rob07b[4], FUN=function (x) x[1,1]))  ## all successful
       , unlist (lapply (mN800Rob07b[4], FUN=function (x) x[2,1]))  ## some successful
       , unlist (lapply (mN800Rob07b[4], FUN=function (x) x[3,1]))  ## none successful
       , unlist (lapply (mN800Rob07b[5], FUN=function (x) x[1,1]))  ## all repressed
       , unlist (lapply (mN800Rob07b[5], FUN=function (x) x[2,1]))  ## some repressed
       , unlist (lapply (mN800Rob07b[5], FUN=function (x) x[3,1]))  ## none repressed
  )
  
  ## 800km, sum + size
  , c (unlist (lapply (mN800Rob07b[17], FUN=function (x) x[2,1]))
       , rep (NA, 6)
       , unlist (lapply (mN800Rob07b[2], FUN=function (x) x[1,1]))
       , rep (NA, 6))
  , c (unlist (lapply (mN800Rob07b[18], FUN=function (x) x[2,1]))
       , rep (NA, 6)
       , unlist (lapply (mN800Rob07b[3], FUN=function (x) x[1,1]))
       , rep (NA, 6))
)
pvalRob07b <- cbind (
  
  ## 50km, dummy
  
  # logit
  c (unlist (lapply (mContRob07b[16], FUN=function (x) x[2,4])) ## within 30 days
     , unlist (lapply (mContRob07b[19], FUN=function (x) x[2,4])) ## all successful
     , unlist (lapply (mContRob07b[19], FUN=function (x) x[3,4])) ## some successful
     , unlist (lapply (mContRob07b[19], FUN=function (x) x[4,4])) ## none successful
     , unlist (lapply (mContRob07b[20], FUN=function (x) x[2,4])) ## all repressed
     , unlist (lapply (mContRob07b[20], FUN=function (x) x[3,4])) ## some repressed
     , unlist (lapply (mContRob07b[20], FUN=function (x) x[4,4])) ## none repressed
     
     # LPM's
     , unlist (lapply (mContRob07b[1], FUN=function (x) x[1,4]))  ## within 30 days
     , unlist (lapply (mContRob07b[4], FUN=function (x) x[1,4]))  ## all successful
     , unlist (lapply (mContRob07b[4], FUN=function (x) x[2,4]))  ## some successful
     , unlist (lapply (mContRob07b[4], FUN=function (x) x[3,4]))  ## none successful
     , unlist (lapply (mContRob07b[5], FUN=function (x) x[1,4]))  ## all repressed
     , unlist (lapply (mContRob07b[5], FUN=function (x) x[2,4]))  ## some repressed
     , unlist (lapply (mContRob07b[5], FUN=function (x) x[3,4]))  ## none repressed
  )
  
  ## 50km, sum + size
  , c (unlist (lapply (mContRob07b[17], FUN=function (x) x[2,4]))
       , rep (NA, 6)
       , unlist (lapply (mContRob07b[2], FUN=function (x) x[1,4]))
       , rep (NA, 6))
  , c (unlist (lapply (mContRob07b[18], FUN=function (x) x[2,4]))
       , rep (NA, 6)
       , unlist (lapply (mContRob07b[3], FUN=function (x) x[1,4]))
       , rep (NA, 6))
  
  
  ## 800km, dummy
  
  # logit
  , c (unlist (lapply (mN800Rob07b[16], FUN=function (x) x[2,4])) ## within 30 days
       , unlist (lapply (mN800Rob07b[19], FUN=function (x) x[2,4])) ## all successful
       , unlist (lapply (mN800Rob07b[19], FUN=function (x) x[3,4])) ## some successful
       , unlist (lapply (mN800Rob07b[19], FUN=function (x) x[4,4])) ## none successful
       , unlist (lapply (mN800Rob07b[20], FUN=function (x) x[2,4])) ## all repressed
       , unlist (lapply (mN800Rob07b[20], FUN=function (x) x[3,4])) ## some repressed
       , unlist (lapply (mN800Rob07b[20], FUN=function (x) x[4,4])) ## none repressed
       
       # LPM's
       , unlist (lapply (mN800Rob07b[1], FUN=function (x) x[1,4]))  ## within 30 days
       , unlist (lapply (mN800Rob07b[4], FUN=function (x) x[1,4]))  ## all successful
       , unlist (lapply (mN800Rob07b[4], FUN=function (x) x[2,4]))  ## some successful
       , unlist (lapply (mN800Rob07b[4], FUN=function (x) x[3,4]))  ## none successful
       , unlist (lapply (mN800Rob07b[5], FUN=function (x) x[1,4]))  ## all repressed
       , unlist (lapply (mN800Rob07b[5], FUN=function (x) x[2,4]))  ## some repressed
       , unlist (lapply (mN800Rob07b[5], FUN=function (x) x[3,4]))  ## none repressed
  )
  
  ## 800km, sum + size
  , c (unlist (lapply (mN800Rob07b[17], FUN=function (x) x[2,4]))
       , rep (NA, 6)
       , unlist (lapply (mN800Rob07b[2], FUN=function (x) x[1,4]))
       , rep (NA, 6))
  , c (unlist (lapply (mN800Rob07b[18], FUN=function (x) x[2,4]))
       , rep (NA, 6)
       , unlist (lapply (mN800Rob07b[3], FUN=function (x) x[1,4]))
       , rep (NA, 6))
)

# retrieving the estimates
coefRob07b <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob07b), 3)), ncol=6))
coefRob07b[coefRob07b=="NA"] <- ""  ## to get rid of the NA's
pvalRob07b <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob07b, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob07b<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob07b), ncol=6)
pvalRob07b[pvalRob07b=="(NA)NA"] <- ""
tabRob07b <- rbind (coefRob07b, pvalRob07b)[c (1, 1+nrow (coefRob07b)) + sort (rep ((0:(nrow (coefRob07b)-1)), 2)),]
tabRob07b <- cbind (tabRob07b[,1:3]
                    , rep ("", nrow (tabRob07b))
                    , tabRob07b[,4:6])

# building the table
rows <- rep (c ("\\emph{Neighbor Democracy Protest}", "~~~~~\\emph{(last election)}"
                , "\\emph{All Successful}", ""
                , "\\emph{Some Successful}", ""
                , "\\emph{None Successful}", ""
                , "\\emph{All Repressed}", ""
                , "\\emph{Some Repressed}", ""
                , "\\emph{None Repressed}", ""), 2)

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n")
Header2 <- paste ("\\multicolumn{1}{l}{\\textbf{(a) \\emph{Logit, country REs}}} & \\multicolumn{1}{c}{\\textbf{dummy}} & \\multicolumn{1}{c}{\\textbf{sum}} & \\multicolumn{1}{c}{\\textbf{size}} & & \\multicolumn{1}{c}{\\textbf{dummy}} & \\multicolumn{1}{c}{\\textbf{sum}} & \\multicolumn{1}{c}{\\textbf{size}} \\\\ \\midrule \n")
Header3 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{(b) \\emph{LPM, country REs}}} \\\\ \\midrule \n")
Header4 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\underline{Protest success}} \\\\ [1.5ex] \n")
Header5 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\underline{Government repression}} \\\\ [1.5ex] \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 2
addtorow$pos[[4]] <- 8
addtorow$pos[[5]] <- 14
addtorow$pos[[6]] <- 16
addtorow$pos[[7]] <- 22
addtorow$command <- c (Header1, Header2, Header4, Header5
                       , Header3, Header4, Header5)
print (xtable (cbind (rows, tabRob07b)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Election-related Protest in Last Election in Neighbor, 2000-2005 Only"
               , label="T:resultsElections03b")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.65) Figure 3: Graphs with marginal effects for interactions (Bunce results)
margBunce <- as.data.frame (cbind (
  c (rep ("50km neighbors", 14), rep ("800km neighbors", 14))
  , c (rep ("(a) main results (50km)", 2), rep ("(b) protest success (50km)", 6), rep ("(c) repressed protests (50km)", 6), rep ("(d) main results (800km)", 2), rep ("(e) protest success (800km)", 6), rep ("(f) repressed protests (800km)", 6))
  , rep (c ("no election", "election", rep ("no election", 3), rep ("election", 3), rep ("no election", 3), rep ("election", 3)), 2)
  , rep (c (rep ("all", 2), rep (c ("all", "some", "none"), 2), rep (c ("all", "some", "none"), 2)), 2)
))
colnames (margBunce) <- c ("nType", "panel", "election", "effect")
margBunce$panel <- factor (margBunce$panel, levels=c ("(a) main results (50km)", "(b) protest success (50km)", "(c) repressed protests (50km)", "(d) main results (800km)", "(e) protest success (800km)", "(f) repressed protests (800km)"))
margBunce$effect <- factor (margBunce$effect, levels=c ("all", "some", "none"))

# getting the marginal effects
margBunce$mEffect <- c (
  unlist (lapply (mContRob07[31], FUN=function (x) x[1,1]))
  , unlist (lapply (mContRob07[31], FUN=function (x) x[1,1]))+unlist (lapply (mContRob07[31], FUN=function (x) x[3,1]))
  , unlist (lapply (mContRob07[34], FUN=function (x) x[1:3,1]))
  , unlist (lapply (mContRob07[34], FUN=function (x) x[1:3,1]))+unlist (lapply (mContRob07[34], FUN=function (x) x[5:7,1]))
  , unlist (lapply (mContRob07[35], FUN=function (x) x[1:3,1]))
  , unlist (lapply (mContRob07[35], FUN=function (x) x[1:3,1]))+unlist (lapply (mContRob07[35], FUN=function (x) x[5:7,1]))
  , unlist (lapply (mN800Rob07[31], FUN=function (x) x[1,1]))
  , unlist (lapply (mN800Rob07[31], FUN=function (x) x[1,1]))+unlist (lapply (mN800Rob07[31], FUN=function (x) x[3,1]))
  , unlist (lapply (mN800Rob07[34], FUN=function (x) x[1:3,1]))
  , unlist (lapply (mN800Rob07[34], FUN=function (x) x[1:3,1]))+unlist (lapply (mN800Rob07[34], FUN=function (x) x[5:7,1]))
  , unlist (lapply (mN800Rob07[35], FUN=function (x) x[1:3,1]))
  , unlist (lapply (mN800Rob07[35], FUN=function (x) x[1:3,1]))+unlist (lapply (mN800Rob07[35], FUN=function (x) x[5:7,1]))
)/100

# getting the standard errors
margBunce$se <- c (
  sqrt (unlist (lapply (mContRob07.vcov[61], FUN=function (x) x[1,1])))
  , sqrt (unlist (lapply (mContRob07.vcov[61], FUN=function (x) x[1,1]))+unlist (lapply (mContRob07.vcov[61], FUN=function (x) x[3,3]))+2*unlist (lapply (mContRob07.vcov[61], FUN=function (x) x[1,3])))
  , sqrt (unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[1,1])))
  , sqrt (unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[2,2])))
  , sqrt (unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[3,3])))
  , sqrt (unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[1,1]))+unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[5,5]))+2*unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[1,5])))
  , sqrt (unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[2,2]))+unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[6,6]))+2*unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[2,6])))
  , sqrt (unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[3,3]))+unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[7,7]))+2*unlist (lapply (mContRob07.vcov[64], FUN=function (x) x[3,7])))
  , sqrt (unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[1,1])))
  , sqrt (unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[2,2])))
  , sqrt (unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[3,3])))
  , sqrt (unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[1,1]))+unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[5,5]))+2*unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[1,5])))
  , sqrt (unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[2,2]))+unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[6,6]))+2*unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[2,6])))
  , sqrt (unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[3,3]))+unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[7,7]))+2*unlist (lapply (mContRob07.vcov[65], FUN=function (x) x[3,7])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[61], FUN=function (x) x[1,1])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[61], FUN=function (x) x[1,1]))+unlist (lapply (mN800Rob07.vcov[61], FUN=function (x) x[3,3]))+2*unlist (lapply (mN800Rob07.vcov[61], FUN=function (x) x[1,3])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[1,1])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[2,2])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[3,3])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[1,1]))+unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[5,5]))+2*unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[1,5])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[2,2]))+unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[6,6]))+2*unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[2,6])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[3,3]))+unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[7,7]))+2*unlist (lapply (mN800Rob07.vcov[64], FUN=function (x) x[3,7])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[1,1])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[2,2])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[3,3])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[1,1]))+unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[5,5]))+2*unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[1,5])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[2,2]))+unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[6,6]))+2*unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[2,6])))
  , sqrt (unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[3,3]))+unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[7,7]))+2*unlist (lapply (mN800Rob07.vcov[65], FUN=function (x) x[3,7])))
)/100
margBunce$low95 <- with (margBunce, mEffect-1.96*se)
margBunce$high95 <- with (margBunce, mEffect+1.96*se)

## Making the plot(s)
cex.points <- 3.5
cex.text <- 16
col.line <- "gray66"
col.soft <- "gray60"
col.dark <- "gray10"
col.main <- "darkturquoise"
cex.bars <- 0.045*3
pd <- position_dodge (width = 0.5)

(margPlot <- ggplot (margBunce, aes (y=mEffect, x=election, colour=effect, group=effect, fill=effect, shape=effect))
  + geom_hline (yintercept=0, colour=col.line, linetype=2)
  + geom_point (size=cex.points, position=pd)
  + geom_errorbar (aes (ymin=low95, ymax=high95), position=pd, size=cex.points/4, width=cex.bars)
  + ylab ("Marginal effect of Democracy Protests")
  + xlab ("")
  + theme_bw ()
  + facet_wrap ( ~ panel)
  + theme_ipsum (base_size=15, axis_title_size=18)
  + theme (legend.position = "bottom", legend.title = element_blank ())
)

# exporting the plot
plotWidth <- 720
plotHeight <- 420

png ("Figures/fig_Bunce.png", w=plotWidth, h=plotHeight)
margPlot
dev.off ()

### (2.66) Table A65: Effect of Neighbor Protests (within 360 Days) on Civil Liberties and Media Freedom, 1989-2011

# getting the coefficients
coefRob08 <- cbind (rbind (
  
  ## contiguous neighbors
  
  # civil liberties
  unlist (lapply (mContRob08[7:9], FUN=function (x) x[1,1]))  ## country FE
  , unlist (lapply (mContRob08[13:15], FUN=function (x) x[1,1]))  ## country FE + controls
  
  # media freedom
  , unlist (lapply (mContRob08[43:45], FUN=function (x) x[1,1]))  ## country FE
  , unlist (lapply (mContRob08[49:51], FUN=function (x) x[1,1]))  ## country FE + controls
)

## 800-km neighbors
, rbind (
  
  # civil liberties
  unlist (lapply (mN800Rob08[7:9], FUN=function (x) x[1,1]))  ## country FE
  , unlist (lapply (mN800Rob08[13:15], FUN=function (x) x[1,1]))  ## country FE + controls
  
  # media freedom
  , unlist (lapply (mN800Rob08[43:45], FUN=function (x) x[1,1]))  ## country FE
  , unlist (lapply (mN800Rob08[49:51], FUN=function (x) x[1,1]))  ## country FE + controls
))
pvalRob08 <- cbind (rbind (
  
  ## contiguous neighbors
  
  # civil liberties
  unlist (lapply (mContRob08[7:9], FUN=function (x) x[1,4]))  ## country FE
  , unlist (lapply (mContRob08[13:15], FUN=function (x) x[1,4]))  ## country FE + controls
  
  # media freedom
  , unlist (lapply (mContRob08[43:45], FUN=function (x) x[1,4]))  ## country FE
  , unlist (lapply (mContRob08[49:51], FUN=function (x) x[1,4]))  ## country FE + controls
)

## 800-km neighbors
, rbind (
  
  # civil liberties
  unlist (lapply (mN800Rob08[7:9], FUN=function (x) x[1,4]))  ## country FE
  , unlist (lapply (mN800Rob08[13:15], FUN=function (x) x[1,4]))  ## country FE + controls
  
  # media freedom
  , unlist (lapply (mN800Rob08[43:45], FUN=function (x) x[1,4]))  ## country FE
  , unlist (lapply (mN800Rob08[49:51], FUN=function (x) x[1,4]))  ## country FE + controls
))

# retrieving the estimates
coefRob08 <- cbind (matrix (sprintf ("%.3f", round (as.matrix (coefRob08), 3)), ncol=6))
pvalRob08 <- matrix (paste (
  paste ("(", sprintf ("%.3f", round (pvalRob08, 3)), ")", sep="")
  , ifelse (as.numeric (pvalRob08<=0.05)==0, "", "*")
  , sep="")
  , nrow=nrow (coefRob08), ncol=6)
tabRob08 <- rbind (coefRob08, pvalRob08)[c (1, 1+nrow (coefRob08)) + sort (rep ((0:(nrow (coefRob08)-1)), 2)),]
tabRob08 <- cbind (tabRob08[,1:3]
                   , rep ("", nrow (tabRob08))
                   , tabRob08[,4:6])

# building the table
rows <- rep (c ("\\emph{Neighbor Democracy Protest}", "~~~~~(last 360 days)"), 4)

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \n")
Header2 <- paste ("& \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsContRob08.low[1]), "-", sprintf ("%.0f", nobsContRob08.high[1]), " countries; ", sprintf ("%.0f", nobsContRob08.low[2]), "-", sprintf ("%.0f", nobsContRob08.high[2]), " obs.)} & & \\multicolumn{3}{c}{\\scriptsize (", sprintf ("%.0f", nobsN800Rob08.low[1]), "-", sprintf ("%.0f", nobsN800Rob08.high[1]), " countries; ", sprintf ("%.0f", nobsN800Rob08.low[2]), "-", sprintf ("%.0f", nobsN800Rob08.high[2]), " obs.)} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n", sep="")
Header3 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{DV: Civil Liberties}}} & \\multicolumn{1}{c}{\\textbf{dummy}} & \\multicolumn{1}{c}{\\textbf{sum}} & \\multicolumn{1}{c}{\\textbf{size}} & & \\multicolumn{1}{c}{\\textbf{dummy}} & \\multicolumn{1}{c}{\\textbf{sum}} & \\multicolumn{1}{c}{\\textbf{size}} \\\\ \\midrule \n")
Header4 <- paste ("[1.5ex] \\multicolumn{8}{l}{\\textbf{\\emph{DV: Media Freedom}}} \\\\ \\midrule \n")
Header5 <- paste ("\\multicolumn{8}{l}{\\underline{\\emph{Country FEs}}} \\\\ [1.0ex] \n")
Header6 <- paste ("[1.0ex] \\multicolumn{8}{l}{\\underline{\\emph{Country FEs $+$ Controls}}} \\\\ [1.0ex] \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 4
addtorow$pos[[7]] <- 4
addtorow$pos[[8]] <- 6
addtorow$command <- c (Header1, Header2, Header3
                       , Header5, Header6
                       , Header4, Header5, Header6)
print (xtable (cbind (rows, tabRob08)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf Effect of Neighbor Protests (within 360 Days) on Civil Liberties and Media Freedom, 1989-2011"
               , label="T:resultsMedia")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


### (2.67) Table A66: % of Observations with Zeroes in the Similarity Measures

# 50-km neighbors
(pct50a_gdp <- round (nrow (base[!is.na (base$n50_d45_wrelAbsGDPcap_any) & base$n50_d45_wrelAbsGDPcap_any==0,]) / nrow (base[!is.na (base$n50_d45_wrelAbsGDPcap_any),])*100,2))
(pct50a_gdp2 <- round (nrow (base[!is.na (base$n50_d45_wrelAbsGDPcap_any) & base$n50_d45_wequal_any==0,]) / nrow (base[!is.na (base$n50_d45_wrelAbsGDPcap_any),])*100,2))
(pct50b_gdp <- round (nrow (base[!is.na (base$n50_d90_wrelAbsGDPcap_any) & base$n50_d90_wrelAbsGDPcap_any==0,]) / nrow (base[!is.na (base$n50_d90_wrelAbsGDPcap_any),])*100,2))
(pct50b_gdp2 <- round (nrow (base[!is.na (base$n50_d90_wrelAbsGDPcap_any) & base$n50_d90_wequal_any==0,]) / nrow (base[!is.na (base$n50_d90_wrelAbsGDPcap_any),])*100,2))
(pct50c_gdp <- round (nrow (base[!is.na (base$n50_d120_wrelAbsGDPcap_any) & base$n50_d120_wrelAbsGDPcap_any==0,]) / nrow (base[!is.na (base$n50_d120_wrelAbsGDPcap_any),])*100,2))
(pct50c_gdp2 <- round (nrow (base[!is.na (base$n50_d120_wrelAbsGDPcap_any) & base$n50_d120_wequal_any==0,]) / nrow (base[!is.na (base$n50_d120_wrelAbsGDPcap_any),])*100,2))

(pct50a_pop <- round (nrow (base[!is.na (base$n50_d45_wrelAbsPop_any) & base$n50_d45_wrelAbsPop_any==0,]) / nrow (base[!is.na (base$n50_d45_wrelAbsPop_any),])*100,2))
(pct50a_pop2 <- round (nrow (base[!is.na (base$n50_d45_wrelAbsPop_any) & base$n50_d45_wequal_any==0,]) / nrow (base[!is.na (base$n50_d45_wrelAbsPop_any),])*100,2))
(pct50b_pop <- round (nrow (base[!is.na (base$n50_d90_wrelAbsPop_any) & base$n50_d90_wrelAbsPop_any==0,]) / nrow (base[!is.na (base$n50_d90_wrelAbsPop_any),])*100,2))
(pct50b_pop2 <- round (nrow (base[!is.na (base$n50_d90_wrelAbsPop_any) & base$n50_d90_wequal_any==0,]) / nrow (base[!is.na (base$n50_d90_wrelAbsPop_any),])*100,2))
(pct50c_pop <- round (nrow (base[!is.na (base$n50_d120_wrelAbsPop_any) & base$n50_d120_wrelAbsPop_any==0,]) / nrow (base[!is.na (base$n50_d120_wrelAbsPop_any),])*100,2))
(pct50c_pop2 <- round (nrow (base[!is.na (base$n50_d120_wrelAbsPop_any) & base$n50_d120_wequal_any==0,]) / nrow (base[!is.na (base$n50_d120_wrelAbsPop_any),])*100,2))

(pct50a_repr <- round (nrow (base[!is.na (base$n50_d45_wrelAbsRepr_any) & base$n50_d45_wrelAbsRepr_any==0,]) / nrow (base[!is.na (base$n50_d45_wrelAbsRepr_any),])*100,2))
(pct50a_repr2 <- round (nrow (base[!is.na (base$n50_d45_wrelAbsRepr_any) & base$n50_d45_wequal_any==0,]) / nrow (base[!is.na (base$n50_d45_wrelAbsRepr_any),])*100,2))
(pct50b_repr <- round (nrow (base[!is.na (base$n50_d90_wrelAbsRepr_any) & base$n50_d90_wrelAbsRepr_any==0,]) / nrow (base[!is.na (base$n50_d90_wrelAbsRepr_any),])*100,2))
(pct50b_repr2 <- round (nrow (base[!is.na (base$n50_d90_wrelAbsRepr_any) & base$n50_d90_wequal_any==0,]) / nrow (base[!is.na (base$n50_d90_wrelAbsRepr_any),])*100,2))
(pct50c_repr <- round (nrow (base[!is.na (base$n50_d120_wrelAbsRepr_any) & base$n50_d120_wrelAbsRepr_any==0,]) / nrow (base[!is.na (base$n50_d120_wrelAbsRepr_any),])*100,2))
(pct50c_repr2 <- round (nrow (base[!is.na (base$n50_d120_wrelAbsRepr_any) & base$n50_d120_wequal_any==0,]) / nrow (base[!is.na (base$n50_d120_wrelAbsRepr_any),])*100,2))

(pct50a_piv <- round (nrow (base[!is.na (base$n50_d45_wrelAbsPolity_any) & base$n50_d45_wrelAbsPolity_any==0,]) / nrow (base[!is.na (base$n50_d45_wrelAbsPolity_any),])*100,2))
(pct50a_piv2 <- round (nrow (base[!is.na (base$n50_d45_wrelAbsPolity_any) & base$n50_d45_wequal_any==0,]) / nrow (base[!is.na (base$n50_d45_wrelAbsPolity_any),])*100,2))
(pct50b_piv <- round (nrow (base[!is.na (base$n50_d90_wrelAbsPolity_any) & base$n50_d90_wrelAbsPolity_any==0,]) / nrow (base[!is.na (base$n50_d90_wrelAbsPolity_any),])*100,2))
(pct50b_piv2 <- round (nrow (base[!is.na (base$n50_d90_wrelAbsPolity_any) & base$n50_d90_wequal_any==0,]) / nrow (base[!is.na (base$n50_d90_wrelAbsPolity_any),])*100,2))
(pct50c_piv <- round (nrow (base[!is.na (base$n50_d120_wrelAbsPolity_any) & base$n50_d120_wrelAbsPolity_any==0,]) / nrow (base[!is.na (base$n50_d120_wrelAbsPolity_any),])*100,2))
(pct50c_piv2 <- round (nrow (base[!is.na (base$n50_d120_wrelAbsPolity_any) & base$n50_d120_wequal_any==0,]) / nrow (base[!is.na (base$n50_d120_wrelAbsPolity_any),])*100,2))

# 800-km neighbors
(pct800a_gdp <- round (nrow (base[!is.na (base$n800_d45_wrelAbsGDPcap_any) & base$n800_d45_wrelAbsGDPcap_any==0,]) / nrow (base[!is.na (base$n800_d45_wrelAbsGDPcap_any),])*100,2))
(pct800a_gdp2 <- round (nrow (base[!is.na (base$n800_d45_wrelAbsGDPcap_any) & base$n800_d45_wequal_any==0,]) / nrow (base[!is.na (base$n800_d45_wrelAbsGDPcap_any),])*100,2))
(pct800b_gdp <- round (nrow (base[!is.na (base$n800_d90_wrelAbsGDPcap_any) & base$n800_d90_wrelAbsGDPcap_any==0,]) / nrow (base[!is.na (base$n800_d90_wrelAbsGDPcap_any),])*100,2))
(pct800b_gdp2 <- round (nrow (base[!is.na (base$n800_d90_wrelAbsGDPcap_any) & base$n800_d90_wequal_any==0,]) / nrow (base[!is.na (base$n800_d90_wrelAbsGDPcap_any),])*100,2))
(pct800c_gdp <- round (nrow (base[!is.na (base$n800_d120_wrelAbsGDPcap_any) & base$n800_d120_wrelAbsGDPcap_any==0,]) / nrow (base[!is.na (base$n800_d120_wrelAbsGDPcap_any),])*100,2))
(pct800c_gdp2 <- round (nrow (base[!is.na (base$n800_d120_wrelAbsGDPcap_any) & base$n800_d120_wequal_any==0,]) / nrow (base[!is.na (base$n800_d120_wrelAbsGDPcap_any),])*100,2))

(pct800a_pop <- round (nrow (base[!is.na (base$n800_d45_wrelAbsPop_any) & base$n800_d45_wrelAbsPop_any==0,]) / nrow (base[!is.na (base$n800_d45_wrelAbsPop_any),])*100,2))
(pct800a_pop2 <- round (nrow (base[!is.na (base$n800_d45_wrelAbsPop_any) & base$n800_d45_wequal_any==0,]) / nrow (base[!is.na (base$n800_d45_wrelAbsPop_any),])*100,2))
(pct800b_pop <- round (nrow (base[!is.na (base$n800_d90_wrelAbsPop_any) & base$n800_d90_wrelAbsPop_any==0,]) / nrow (base[!is.na (base$n800_d90_wrelAbsPop_any),])*100,2))
(pct800b_pop2 <- round (nrow (base[!is.na (base$n800_d90_wrelAbsPop_any) & base$n800_d90_wequal_any==0,]) / nrow (base[!is.na (base$n800_d90_wrelAbsPop_any),])*100,2))
(pct800c_pop <- round (nrow (base[!is.na (base$n800_d120_wrelAbsPop_any) & base$n800_d120_wrelAbsPop_any==0,]) / nrow (base[!is.na (base$n800_d120_wrelAbsPop_any),])*100,2))
(pct800c_pop2 <- round (nrow (base[!is.na (base$n800_d120_wrelAbsPop_any) & base$n800_d120_wequal_any==0,]) / nrow (base[!is.na (base$n800_d120_wrelAbsPop_any),])*100,2))

(pct800a_repr <- round (nrow (base[!is.na (base$n800_d45_wrelAbsRepr_any) & base$n800_d45_wrelAbsRepr_any==0,]) / nrow (base[!is.na (base$n800_d45_wrelAbsRepr_any),])*100,2))
(pct800a_repr2 <- round (nrow (base[!is.na (base$n800_d45_wrelAbsRepr_any) & base$n800_d45_wequal_any==0,]) / nrow (base[!is.na (base$n800_d45_wrelAbsRepr_any),])*100,2))
(pct800b_repr <- round (nrow (base[!is.na (base$n800_d90_wrelAbsRepr_any) & base$n800_d90_wrelAbsRepr_any==0,]) / nrow (base[!is.na (base$n800_d90_wrelAbsRepr_any),])*100,2))
(pct800b_repr2 <- round (nrow (base[!is.na (base$n800_d90_wrelAbsRepr_any) & base$n800_d90_wequal_any==0,]) / nrow (base[!is.na (base$n800_d90_wrelAbsRepr_any),])*100,2))
(pct800c_repr <- round (nrow (base[!is.na (base$n800_d120_wrelAbsRepr_any) & base$n800_d120_wrelAbsRepr_any==0,]) / nrow (base[!is.na (base$n800_d120_wrelAbsRepr_any),])*100,2))
(pct800c_repr2 <- round (nrow (base[!is.na (base$n800_d120_wrelAbsRepr_any) & base$n800_d120_wequal_any==0,]) / nrow (base[!is.na (base$n800_d120_wrelAbsRepr_any),])*100,2))

(pct800a_piv <- round (nrow (base[!is.na (base$n800_d45_wrelAbsPolity_any) & base$n800_d45_wrelAbsPolity_any==0,]) / nrow (base[!is.na (base$n800_d45_wrelAbsPolity_any),])*100,2))
(pct800a_piv2 <- round (nrow (base[!is.na (base$n800_d45_wrelAbsPolity_any) & base$n800_d45_wequal_any==0,]) / nrow (base[!is.na (base$n800_d45_wrelAbsPolity_any),])*100,2))
(pct800b_piv <- round (nrow (base[!is.na (base$n800_d90_wrelAbsPolity_any) & base$n800_d90_wrelAbsPolity_any==0,]) / nrow (base[!is.na (base$n800_d90_wrelAbsPolity_any),])*100,2))
(pct800b_piv2 <- round (nrow (base[!is.na (base$n800_d90_wrelAbsPolity_any) & base$n800_d90_wequal_any==0,]) / nrow (base[!is.na (base$n800_d90_wrelAbsPolity_any),])*100,2))
(pct800c_piv <- round (nrow (base[!is.na (base$n800_d120_wrelAbsPolity_any) & base$n800_d120_wrelAbsPolity_any==0,]) / nrow (base[!is.na (base$n800_d120_wrelAbsPolity_any),])*100,2))
(pct800c_piv2 <- round (nrow (base[!is.na (base$n800_d120_wrelAbsPolity_any) & base$n800_d120_wequal_any==0,]) / nrow (base[!is.na (base$n800_d120_wrelAbsPolity_any),])*100,2))

# creating the table
tabZeros <- rbind (
  c (pct50a_gdp, pct50b_gdp, pct50c_gdp, pct800a_gdp, pct800b_gdp, pct800c_gdp)
  , c (pct50a_gdp2, pct50b_gdp2, pct50c_gdp2, pct800a_gdp2, pct800b_gdp2, pct800c_gdp2)
  , c (pct50a_pop, pct50b_pop, pct50c_pop, pct800a_pop, pct800b_pop, pct800c_pop)
  , c (pct50a_pop2, pct50b_pop2, pct50c_pop2, pct800a_pop2, pct800b_pop2, pct800c_pop2)
  , c (pct50a_repr, pct50b_repr, pct50c_repr, pct800a_repr, pct800b_repr, pct800c_repr)
  , c (pct50a_repr2, pct50b_repr2, pct50c_repr2, pct800a_repr2, pct800b_repr2, pct800c_repr2)
  , c (pct50a_piv, pct50b_piv, pct50c_piv, pct800a_piv, pct800b_piv, pct800c_piv)
  , c (pct50a_piv2, pct50b_piv2, pct50c_piv2, pct800a_piv2, pct800b_piv2, pct800c_piv2)
)
(tabZeros2 <- cbind (
  matrix (sprintf ("%.2f", round (tabZeros[,1:3], 2)), ncol=3)
  , rep ("", nrow(tabZeros))
  , matrix (sprintf ("%.2f", round (tabZeros[,4:6], 2)), ncol=3)))

rows <- rep (c ("zeros (\\%)", "no neighbor protests (\\%)"), 4)

Header1 <- paste ("\\toprule & \\multicolumn{3}{c}{\\textbf{50km Neighbors}} & & \\multicolumn{3}{c}{\\textbf{800km Neighbors}} \\\\ \\cmidrule{2-4} \\cmidrule{6-8} \n", sep="")
Header2 <- paste ("\\multicolumn{1}{l}{\\textbf{\\emph{GDP per capita}}} & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} & & \\multicolumn{1}{c}{\\textbf{45 days}} & \\multicolumn{1}{c}{\\textbf{90 days}} & \\multicolumn{1}{c}{\\textbf{120 days}} \\\\ \\midrule \n")
Header3 <- paste ("[2ex] \\multicolumn{8}{l}{\\textbf{\\emph{Population}}} \\\\ \\midrule \n")
Header4 <- paste ("[2ex] \\multicolumn{8}{l}{\\textbf{\\emph{Repressiveness}}} \\\\ \\midrule \n")
Header5 <- paste ("[2ex] \\multicolumn{8}{l}{\\textbf{\\emph{Polity Scores}}} \\\\ \\midrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 2
addtorow$pos[[4]] <- 4
addtorow$pos[[5]] <- 6
addtorow$command <- c (Header1, Header2, Header3, Header4, Header5)
print (xtable (cbind (rows, tabZeros2)
               , align=c("l","l","c","c","c","c","c","c","c")
               , digits=3
               , caption="\\bf \\% of Observations with Zeros in the Similarity Measures"
               , label="T:zeros")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top"
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)


# saving
# system.time (save.image ("Replication Results BrancatiLucardi JCR Democracy Protests - Tables.RData"))
